/
PBPopupBar.swift
1626 lines (1346 loc) · 64.6 KB
/
PBPopupBar.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// PBPopupBar.swift
// PBPopupController
//
// Created by Patrick BODET on 29/03/2018.
// Copyright © 2018-2022 Patrick BODET. All rights reserved.
//
import UIKit
import ObjectiveC
/**
Available styles for the popup bar.
Use the most appropriate style for the current operating system version. Uses prominent style for iOS 10 and above, otherwise compact.
*/
@objc public enum PBPopupBarStyle : Int {
/**
Prominent bar style
*/
case prominent
/**
Compact bar style
*/
case compact
/**
Custom bar style
- Note: Do not set this style directly. Instead set `PBPopupBar.customBarViewController` and the framework will use this style.
*/
case custom
/**
Default style: prominent style for iOS 10 and above, otherwise compact.
*/
public static let `default`: PBPopupBarStyle = {
return .prominent
}()
}
extension PBPopupBarStyle
{
/**
An array of human readable strings for the popup bar styles.
*/
public static let strings = ["prominent", "compact", "custom"]
private func string() -> NSString {
return PBPopupBarStyle.strings[self.rawValue] as NSString
}
/**
Return an human readable description for the popup bar style.
*/
public var description: NSString {
get {
return string()
}
}
}
/**
Available styles for the progress view.
Use the most appropriate style for the current operating system version. Uses none for iOS 10 and above, otherwise bottom.
*/
@objc public enum PBPopupBarProgressViewStyle : Int {
/**
Progress view on bottom.
*/
case bottom
/**
Progress view on top.
*/
case top
/**
No progress view.
*/
case none
/**
Default style: none for iOS 10 and above, otherwise bottom.
*/
public static let `default`: PBPopupBarProgressViewStyle = {
return .none
}()
}
extension PBPopupBarProgressViewStyle
{
/**
An array of human readable strings for the progress view styles.
*/
public static let strings = ["bottom", "top", "none"]
private func string() -> NSString {
return PBPopupBarProgressViewStyle.strings[self.rawValue] as NSString
}
/**
Return an human readable description for the progress view style.
*/
public var description:NSString {
get {
return string()
}
}
}
/**
Available styles for the border view.
*/
@objc public enum PBPopupBarBorderViewStyle : Int {
/**
Border view on left.
*/
case left
/**
Border view on right.
*/
case right
/**
No border view.
*/
case none
/**
Default style: none.
*/
public static let `default`: PBPopupBarBorderViewStyle = {
return .none
}()
}
extension PBPopupBarBorderViewStyle
{
/**
An array of human readable strings for the border view styles.
*/
public static let strings = ["left", "right", "none"]
private func string() -> NSString {
return PBPopupBarBorderViewStyle.strings[self.rawValue] as NSString
}
/**
Return an human readable description for the progress view style.
*/
public var description:NSString {
get {
return string()
}
}
}
// _UITAMICAdaptorView
private let itemClass11: AnyClass? = NSClassFromString(_PBPopupDecodeBase64String(base64String: "X1VJVEFNSUNBZGFwdG9yVmlldw==")!)
// _UIToolbarNavigationButton
private let itemClass10: AnyClass? = NSClassFromString(_PBPopupDecodeBase64String(base64String: "X1VJVG9vbGJhck5hdmlnYXRpb25CdXR0b24=")!)
internal let PBPopupBarHeightCompact: CGFloat = 48.0
internal let PBPopupBarHeightProminent: CGFloat = 64.5
internal let PBPopupBarImageHeightProminent: CGFloat = 48.0
internal let PBPopupBarImageHeightCompact: CGFloat = 40.0
// MARK: - Public Protocols
/**
The data source providing custom labels instances to the popup bar so they can be used instead of the default provided ones.
*/
@objc public protocol PBPopupBarDataSource: NSObjectProtocol {
/**
Returns a UIlabel subclass object to be used by the popup bar instead of the default title label (for example a MarqueeLabel instance).
- Parameter popupBar: The popup bar object asking for a label.
- returns: A `UIlabel` object to be used instead of the default one.
*/
@objc optional func titleLabel(for popupBar: PBPopupBar) -> UILabel?
/**
Returns a UIlabel subclass object to be used by the popup bar instead of the default subtitle label (for example a MarqueeLabel instance).
- Parameter popupBar: The popup bar object asking for a label.
- returns: A `UIlabel` object to be used instead of the default one.
*/
@objc optional func subtitleLabel(for popupBar: PBPopupBar) -> UILabel?
}
/**
A set of methods used by the delegate to respond, with a preview view controller and a commit view controller, to the user pressing the popup bar object on the screen of a device that supports 3D Touch.
*/
@objc public protocol PBPopupBarPreviewingDelegate: NSObjectProtocol {
/**
Called when the user performs a peek action on the popup bar.
The default implementation returns `nil` and no preview is displayed.
- Parameter popupBar: The popup bar object.
- returns: The view controller whose view you want to provide as the preview (peek), or `nil` to disable preview.
*/
@objc optional func previewingViewControllerFor(_ popupBar: PBPopupBar) -> UIViewController?
/**
Called when the user performs a pop action on the popup bar.
The default implementation does not commit the view controller.
- Parameter popupBar: The popup bar object.
- Parameter viewControllerToCommit: The view controller to commit.
*/
@objc optional func popupBar(_ popupBar: PBPopupBar, commit viewControllerToCommit: UIViewController)
}
/**
A popup bar presented with a content view controller such as a `UITabBarController`, a `UINavigationController`, a `UIViewController` or a custom container view controller. The user can swipe or tap the popup bar at any point to present the popup content view controller. After presenting, the user dismisses the popup content view controller by either swiping or tapping an optional popup close button. The contents of the popup bar is built dynamically using its own properties. The popup bar may be a custom one if `PBPopupBar.customPopupBarViewController` is set.
*/
@objc public class PBPopupBar: UIView {
// MARK: - Public Properties
/**
The data source of the PBPopupBar object.
- SeeAlso: `PBPopupBarDataSource`.
*/
@objc weak public var dataSource: PBPopupBarDataSource? {
didSet {
self.askForLabels = true
}
}
/**
The previewing delegate object mediates the presentation of views from the preview (peek) view controller and the commit (pop) view controller. In practice, these two are typically the same view controller. The delegate performs this mediation through your implementation of the methods of the `PBPopupBarPreviewingDelegate` protocol.
- SeeAlso: `PBPopupBarPreviewingDelegate`.
*/
@objc weak public var previewingDelegate: PBPopupBarPreviewingDelegate?
/**
For debug: If `true`, the popup bar will attribute some colors to its subviews.
*/
@objc public var PBPopupBarShowColors: Bool = false
/**
The popup bar presentation duration when presenting from hidden to closed state.
- Seealso: `PBPopupContentView.popupPresentationDuration`.
*/
@objc public var popupBarPresentationDuration: TimeInterval = 0.6
/**
The tap gesture recognizer attached to the popup bar for presenting the popup content view.
*/
@objc public var popupTapGestureRecognizer: UITapGestureRecognizer!
/**
Set this property with a custom popup bar view controller object to provide a popup bar with custom content. In this custom view controller, use the preferredContentSize property to set the size of the custom popup bar (example: preferredContentSize = CGSize(width: -1, height: 65)).
*/
@objc public var customPopupBarViewController: UIViewController? {
willSet {
PBLog("The value of customBarViewController will change from \(String(describing: customPopupBarViewController)) to \(String(describing: newValue))")
customPopupBarViewController?.view.removeFromSuperview()
}
didSet {
PBLog("The value of customBarViewController changed from \(String(describing: oldValue)) to \(String(describing: customPopupBarViewController))")
if customPopupBarViewController != nil {
customPopupBarViewController!.popupContainerViewController = self.popupController.containerViewController
self.popupBarStyle = .custom
}
}
}
/**
If `true`, the popup bar will automatically inherit its style from the bottom bar.
*/
@objc public var inheritsVisualStyleFromBottomBar: Bool = true {
didSet {
if inheritsVisualStyleFromBottomBar == true {
self.popupController.containerViewController.configurePopupBarFromBottomBar()
}
}
}
/**
The popup bar style (see PBPopupBarStyle).
*/
@objc public var popupBarStyle: PBPopupBarStyle = .default {
willSet {
PBLog("The value of popupBarStyle will change from \(popupBarStyle.description) to \(newValue.description)")
customPopupBarViewController?.view.removeFromSuperview()
}
didSet {
PBLog("The value of popupBarStyle changed from \(oldValue.description) to \(popupBarStyle.description)")
if popupBarStyle == .custom {
if self.customPopupBarViewController == nil {
PBLog("Custom popuBar view controller cannot be nil.", error: true)
}
assert(self.customPopupBarViewController != nil, "Custom popuBar view controller cannot be nil.")
if self.customPopupBarViewController == nil {
NSException.raise(NSExceptionName.internalInconsistencyException, format: "Custom popuBar view controller cannot be nil.", arguments: getVaList([]))
}
}
self.setupCustomPopupBarView()
self.layoutIfNeeded()
let effect = UIBlurEffect(style: self.backgroundStyle)
self.backgroundView.effect = effect
self.layoutToolbarItems()
self.configureTitleLabels()
if self.popupController.popupPresentationState != .hidden && oldValue != popupBarStyle {
self.popupController.popupPresentationState = .hidden
if let vc = self.popupController.containerViewController {
let height = oldValue == .custom ? -(customPopupBarViewController?.preferredContentSize.height ?? 0) : oldValue == .prominent ? -PBPopupBarHeightProminent : -PBPopupBarHeightCompact
let additionalInsets = UIEdgeInsets(top: self.popupController.wantsAdditionalSafeAreaInsetTop ? height : 0, left: 0, bottom: self.popupController.wantsAdditionalSafeAreaInsetBottom ? height : 0, right: 0)
PBPopupFixInsetsForViewController(vc, false, additionalInsets)
self.popupController._presentPopupBarAnimated(false)
}
}
}
}
/**
The bar style of the popup bar' toolbar.
*/
@objc public var barStyle: UIBarStyle {
get {
return self.toolbar.barStyle
}
set(newValue) {
self.systemBarStyle = newValue
self.popupController.barStyle = newValue
#if targetEnvironment(macCatalyst)
self.backgroundView.backgroundColor = nil
#else
if #available(iOS 13.0, *) {
self.backgroundView.backgroundColor = nil
}
else {
if newValue == .black {
self.backgroundView.backgroundColor = UIColor.clear
}
else {
// Drop iOS 11- look
self.backgroundView.backgroundColor = nil
//self.backgroundView.backgroundColor = UIColor(white: 230.0 / 255.0, alpha: 0.65)
}
}
#endif
self.toolbar.barStyle = newValue
}
}
/**
The popup bar background style that specifies its visual effect appearance.
- SeeAlso: `UIBlurEffect.Style`
*/
@objc public var backgroundStyle: UIBlurEffect.Style {
get {
#if compiler(>=5.1)
if #available(iOS 13.0, *) {
if self.systemBarStyle == .black {
return .systemChromeMaterialDark
}
return .systemChromeMaterial
}
#endif
// Drop iOS 11- look
//return self.systemBarStyle == .black ? .dark : popupBarStyle == .compact ? .extraLight : .light
return self.systemBarStyle == .black ? .dark : .extraLight
}
set {
self.toolbar.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
self.backgroundView.effect = UIBlurEffect(style: newValue)
self.safeAreaToolbar?.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
}
}
/**
A Boolean value that indicates whether the popup bar is translucent (`true`) or not (`false`).
*/
@objc public var isTranslucent: Bool {
get {
return self.toolbar.isTranslucent
}
set {
if self.toolbar.isTranslucent != newValue {
self.toolbar.isTranslucent = newValue
self.backgroundView.isHidden = (newValue == false)
self.safeAreaToolbar?.isTranslucent = newValue
if self.inheritsVisualStyleFromBottomBar == true {
self.backgroundView.effect = nil
self.backgroundView.isHidden = true
}
}
}
}
/**
The background color of the popup bar' toolbar.
*/
@objc override public var backgroundColor: UIColor? {
get {
return self.toolbar.backgroundColor
}
set {
if self.toolbar.backgroundColor != newValue {
self.toolbar.setBackgroundImage(newValue == nil ? UIImage() : nil, forToolbarPosition: .any, barMetrics: .default)
self.toolbar.backgroundColor = newValue
self.safeAreaToolbar?.setBackgroundImage(newValue == nil ? UIImage() : nil, forToolbarPosition: .any, barMetrics: .default)
self.safeAreaToolbar?.backgroundColor = newValue
}
}
}
/**
The tint color to apply to the popup bar background.
*/
@objc public var barTintColor: UIColor! {
get {
return self.toolbar.barTintColor
}
set {
if self.toolbar.barTintColor != newValue {
self.toolbar.setBackgroundImage(newValue == nil ? UIImage() : nil, forToolbarPosition: .any, barMetrics: .default)
self.toolbar.barTintColor = newValue
self.safeAreaToolbar?.setBackgroundImage(newValue == nil ? UIImage() : nil, forToolbarPosition: .any, barMetrics: .default)
self.safeAreaToolbar?.barTintColor = newValue
}
}
}
/**
The tint color to apply to the popup bar items.
*/
@objc override public var tintColor: UIColor! {
get {
return self.toolbar.tintColor
}
set {
if self.toolbar.tintColor != newValue {
self.toolbar.tintColor = newValue
}
}
}
/**
The popup bar's image.
- note: The image will only be displayed on prominent popup bars.
*/
@objc public var image: UIImage? = nil {
didSet {
self.shadowImageView.imageView.image = image
self.layoutImageView()
}
}
/**
An image view displayed when the bar style is prominent. (read-only)
*/
@objc public internal(set) var imageView: UIImageView!
/**
The view providing a shadow' layer to the popup bar image view.
- Note: Read-only, but its properties can be set. For example for no shadow, use `popupBar.shadowImageView.shadowOpacity = 0`.
*/
@objc public private(set) var shadowImageView: PBPopupRoundShadowImageView!
/**
The popup bar's title.
- Note: If no subtitle is set, the title will be centered vertically.
*/
@objc public var title: String? {
didSet {
self.titleLabel.isHidden = title == nil
self.configureTitleLabels()
self.configureAccessibility()
}
}
/**
Display attributes for the popup bar’s title text.
You may specify the font, text color, and shadow properties for the title in the text attributes dictionary, using the keys found in `NSAttributedString.h`.
*/
@objc public var titleTextAttributes: [NSAttributedString.Key: Any]? {
didSet {
self.configureTitleLabels()
}
}
/**
The popup bar's subtitle.
- Note: If no title is set, the subtitle will be centered vertically.
*/
@objc public var subtitle: String? {
didSet {
self.subtitleLabel.isHidden = subtitle == nil
self.configureTitleLabels()
self.configureAccessibility()
}
}
/**
Display attributes for the popup bar’s subtitle text.
You may specify the font, text color, and shadow properties for the subtitle in the text attributes dictionary, using the keys found in `NSAttributedString.h`.
*/
@objc public var subtitleTextAttributes: [NSAttributedString.Key: Any]? {
didSet {
self.configureTitleLabels()
}
}
/**
The string that succinctly identifies the accessibility element (titles view, the container for title and subtitle labels).
*/
override public var accessibilityLabel: String? {
get {
return self.titlesView.accessibilityLabel
}
set {
self.titlesView.accessibilityLabel = newValue
self.configureAccessibility()
}
}
/**
The string that briefly describes the result of performing an action on the accessibility title view (container for title and subtitle labels).
*/
override public var accessibilityHint: String? {
get {
return self.titlesView.accessibilityHint
}
set {
self.titlesView.accessibilityHint = newValue
self.configureAccessibility()
}
}
/**
The semantic description of the view’s contents, used to determine whether the view should be flipped when switching between left-to-right and right-to-left layouts.
Defaults to `UISemanticContentAttribute.unspecified`
*/
@objc override public var semanticContentAttribute: UISemanticContentAttribute {
didSet {
self.toolbar.semanticContentAttribute = semanticContentAttribute
self.layoutToolbarItems()
self.configureTitleLabels()
}
}
/**
An array of custom bar button items to display on the left side. Or right side if RTL.
*/
@objc public var leftBarButtonItems: [UIBarButtonItem]? {
didSet {
self.layoutToolbarItems()
self.configureTitleLabels()
}
}
/**
An array of custom bar button items to display on the right side. Or left side if RTL.
*/
@objc public var rightBarButtonItems: [UIBarButtonItem]? {
didSet {
self.layoutToolbarItems()
self.configureTitleLabels()
}
}
/**
The semantic description of the bar items, used to determine the order of bar items when switching between left-to-right and right-to-left layouts.
Defaults to `UISemanticContentAttribute.playback`
*/
@objc public var barButtonItemsSemanticContentAttribute: UISemanticContentAttribute = .playback {
didSet {
self.layoutToolbarItems()
self.configureTitleLabels()
}
}
/**
The popup bar's progress view style.
*/
@objc public var progressViewStyle: PBPopupBarProgressViewStyle = .default {
didSet {
self.layoutProgressView()
}
}
/**
The popup bar progress view's progress.
The progress is represented by a floating-point value between 0.0 and 1.0, inclusive, where 1.0 indicates the completion of the task. The default value is 0.0. Values less than 0.0 and greater than 1.0 are pinned to those limits.
*/
@objc public var progress: Float {
get {
return self.progressView?.progress ?? 0.0
}
set {
self.progressView.setProgress(newValue, animated: false)
}
}
/**
The popup bar's border view style.
*/
@objc public var borderViewStyle: PBPopupBarBorderViewStyle = .default {
didSet {
self.layoutBorderView()
}
}
/**
The accessibility label of the progress, in a localized string.
*/
@objc public var accessibilityProgressLabel: String?
/**
The accessibility value of the progress, in a localized string.
*/
@objc public var accessibilityProgressValue: String?
// MARK: - Private Properties
internal weak var popupController: PBPopupController!
internal var ignoreLayoutDuringTransition: Bool = false
internal var popupBarHeight: CGFloat {
if self.popupBarStyle == .custom {
return customPopupBarViewController != nil ? customPopupBarViewController!.preferredContentSize.height : PBPopupBarHeightProminent
}
return self.popupBarStyle == .prominent ? PBPopupBarHeightProminent : PBPopupBarHeightCompact
}
private var systemBarStyle: UIBarStyle!
private var backgroundView: UIVisualEffectView!
private var toolbar: PBPopupToolbar!
private var toolbarBottomConstraint: NSLayoutConstraint!
private var safeAreaBackgroundViewHeightConstraint: NSLayoutConstraint?
private var safeAreaToolbar: UIToolbar!
private var imageViewTopConstraint: NSLayoutConstraint!
private var imageViewLeftConstraint: NSLayoutConstraint!
private var imageViewRightConstraint: NSLayoutConstraint!
private var imageViewWidthConstraint: NSLayoutConstraint!
private var imageViewHeightConstraint: NSLayoutConstraint!
private var imageController: UIViewController?
internal var imageShadowOpacity: Float = 0.0
internal var swiftUIImageController: UIViewController? {
set {
if let imageController = imageController {
imageController.view.removeFromSuperview()
}
imageController = newValue
imageController?.view.backgroundColor = nil
if let imageController = imageController {
self.shadowImageView.cornerRadius = imageController.value(forKey: "cornerRadius") as! CGFloat
self.shadowImageView.shadowColor = imageController.value(forKey: "shadowColor") as? UIColor
self.shadowImageView.shadowOffset = imageController.value(forKey: "shadowOffset") as! CGSize
self.shadowImageView.shadowOpacity = imageController.value(forKey: "shadowOpacity") as! Float
self.shadowImageView.shadowRadius = imageController.value(forKey: "shadowRadius") as! CGFloat
self.imageShadowOpacity = self.shadowImageView.shadowOpacity
self.shadowImageView.addSubview(imageController.view)
self.shadowImageView.shadowOpacity = 0.0
self.shadowImageView.addSubview(imageController.view)
imageController.view.translatesAutoresizingMaskIntoConstraints = false
imageController.view.topAnchor.constraint(equalTo: self.shadowImageView.topAnchor, constant: 0.0).isActive = true
imageController.view.leftAnchor.constraint(equalTo: self.shadowImageView.leftAnchor, constant: 0.0).isActive = true
imageController.view.rightAnchor.constraint(equalTo: self.shadowImageView.rightAnchor, constant: 0.0).isActive = true
imageController.view.bottomAnchor.constraint(equalTo: self.shadowImageView.bottomAnchor, constant: 0.0).isActive = true
self.layoutImageView()
self.layoutTitlesView()
}
}
get {
return imageController
}
}
// The container view for titleLabel and subtitleLabel
@objc dynamic private var titlesView: PBPopupBarTitlesView!
private var titlesViewLeftConstraint: NSLayoutConstraint!
private var titlesViewRightConstraint: NSLayoutConstraint!
// true if we have to ask for a custom label (i.e. MarqueeLabel)
private var askForLabels: Bool = false
// The label containing the title text
internal var titleLabel: UILabel!
private var titleLabelCenterConstraint: NSLayoutConstraint!
private var titleLabelTopConstraint: NSLayoutConstraint!
private var titleLabelHeightConstraint: NSLayoutConstraint!
// The label containing the subtitle text
internal var subtitleLabel: UILabel!
private var subtitleLabelCenterConstraint: NSLayoutConstraint!
private var subtitleLabelBottomConstraint: NSLayoutConstraint!
private var subtitleLabelHeightConstraint: NSLayoutConstraint!
// The progress view (see PBPopupBarProgressViewStyle and progress property)
@objc dynamic private var progressView: PBPopupBarProgressView!
private var progressViewVerticalConstraints: [NSLayoutConstraint]!
private var progressViewHorizontalConstraints: [NSLayoutConstraint]!
// Highlighted view when taping or paning the popupBar
@objc dynamic internal var highlightView: PBPopupBarHighlightView!
// Border view for iPad when the popup bar is the neighbour of another object
@objc dynamic internal var borderView: PBPopupBarBorderView!
private var borderViewVerticalConstraints: [NSLayoutConstraint]!
private var borderViewHorizontalConstraints: [NSLayoutConstraint]!
// MARK: - Private Init
required internal init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
internal override init(frame: CGRect) {
super.init(frame: frame)
self.autoresizesSubviews = true // default: true
self.preservesSuperviewLayoutMargins = true // default: false
self.autoresizingMask = []
let effect = UIBlurEffect(style: self.backgroundStyle)
self.backgroundView = UIVisualEffectView(effect: effect)
self.backgroundView.clipsToBounds = false
self.backgroundView.autoresizingMask = []
self.backgroundView.isUserInteractionEnabled = false
if #available(iOS 13.0, *) {
self.backgroundView.backgroundColor = nil
}
else {
// Drop iOS 11- look
self.backgroundView.backgroundColor = nil
//self.backgroundView.backgroundColor = UIColor(white: 230.0 / 255.0, alpha: 0.65)
}
self.addSubview(self.backgroundView)
#if !targetEnvironment(macCatalyst)
self.safeAreaToolbar = UIToolbar()
self.safeAreaToolbar.autoresizingMask = []
self.safeAreaToolbar.isTranslucent = true
self.safeAreaToolbar.setBackgroundImage(UIImage(), forToolbarPosition: .bottom, barMetrics: .default)
self.safeAreaToolbar.setShadowImage(UIImage(), forToolbarPosition: .topAttached)
self.safeAreaToolbar.clipsToBounds = false
self.addSubview(self.safeAreaToolbar)
#endif
self.toolbar = PBPopupToolbar(frame: self.bounds)
self.toolbar.autoresizingMask = []
self.toolbar.isTranslucent = true
// For background color, we have to replace the system image.
// -- > self.toolbar.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
// The backgroundColor is not translucent.
// For barTintColor, do not replace the system image.
// --> self.toolbar.setBackgroundImage(nil, forToolbarPosition: .any, barMetrics: .default)
// The barTintColor is translucent if the toolbar is translucent.
// For effect view, replace the system image.
// --> self.toolbar.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
self.toolbar.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
// To remove the top line:
// --> self.toolbar.clipsToBounds = true
// Or if toolbar.clipsToBounds = false
// --> self.toolbar.setShadowImage(UIImage(), forToolbarPosition: .topAttached)
// 1 - Must be called before clipsToBounds else the shadow line not shown
self.toolbar.layer.masksToBounds = true
// 2 - For the shadow image (top line)
self.toolbar.clipsToBounds = false
self.addSubview(self.toolbar)
self.shadowImageView = PBPopupRoundShadowImageView(frame: .zero)
self.shadowImageView.cornerRadius = 3.0
self.shadowImageView.shadowColor = UIColor.black
self.shadowImageView.shadowOffset = CGSize(width: 0.0, height: 3.0)
self.shadowImageView.shadowRadius = 3.0
self.shadowImageView.shadowOpacity = 0.5
self.shadowImageView.imageView.accessibilityTraits = UIAccessibilityTraits.image
self.shadowImageView.imageView.isAccessibilityElement = true
self.shadowImageView.imageView.contentMode = .scaleAspectFit
self.shadowImageView.contentMode = .scaleAspectFit
self.shadowImageView.translatesAutoresizingMaskIntoConstraints = false
self.toolbar.addSubview(self.shadowImageView)
self.titlesView = PBPopupBarTitlesView()
self.titlesView.isAccessibilityElement = true
self.titlesView.accessibilityTraits = .button
self.titlesView.accessibilityLabel = NSLocalizedString("Popup bar", comment: "")
self.titlesView.autoresizingMask = []
self.titlesView.isUserInteractionEnabled = false
self.toolbar.addSubview(self.titlesView)
self.titleLabel = UILabel()
self.titleLabel.isAccessibilityElement = true
self.titleLabel.backgroundColor = UIColor.clear
self.titleLabel.isHidden = true
self.titleLabel.setContentHuggingPriority(.required, for: .vertical)
self.titlesView.addArrangedSubview(self.titleLabel)
self.subtitleLabel = UILabel()
self.subtitleLabel.isAccessibilityElement = true
self.subtitleLabel.backgroundColor = UIColor.clear
self.subtitleLabel.isHidden = true
self.subtitleLabel.setContentHuggingPriority(.required, for: .vertical)
self.titlesView.addArrangedSubview(self.subtitleLabel)
self.progressView = PBPopupBarProgressView(progressViewStyle: .default)
self.progressView.trackImage = UIImage()
self.toolbar.addSubview(self.progressView)
self.progressView.setProgress(0.0, animated: false)
self.highlightView = PBPopupBarHighlightView()
self.highlightView.autoresizingMask = []
self.highlightView.isUserInteractionEnabled = false
self.highlightView.backgroundColor = UIColor.black.withAlphaComponent(0.1)
if #available(iOS 13.0, *) {
if self.traitCollection.userInterfaceStyle == .dark/* || self.overrideUserInterfaceStyle == .dark*/ {
self.highlightView.backgroundColor = UIColor.white.withAlphaComponent(0.1)
}
}
self.highlightView.alpha = 0.0
self.addSubview(self.highlightView)
self.borderView = PBPopupBarBorderView()
self.borderView.backgroundColor = UIColor.lightGray
self.addSubview(borderView)
self.isAccessibilityElement = false
self.configureAccessibility()
self.semanticContentAttribute = .unspecified
self.clipsToBounds = false // Important for shadow line.
NotificationCenter.default.addObserver(forName: UIContentSizeCategory.didChangeNotification, object: nil, queue: .main) { [weak self] notification in
self?.configureTitleLabels()
}
}
deinit {
NotificationCenter.default.removeObserver(self, name: UIContentSizeCategory.didChangeNotification, object: nil)
PBLog("deinit \(self)")
}
// MARK: - Public Methods
/**
Call this method to update the popup bar appearance (style, tint color, etc.) according to its docking view. You should call this after updating the docking view.
If the popup bar's `inheritsVisualStyleFromBottomBar` property is set to `false`, this method has no effect.
- SeeAlso: `PBPopupBar.inheritsVisualStyleFromBottomBar`.
*/
@objc public func updatePopupBarAppearance() {
self.popupController.containerViewController.configurePopupBarFromBottomBar()
}
/**
:nodoc:
*/
override public func layoutSubviews() {
super.layoutSubviews()
guard let popupBarView = self.superview else {return}
if self.ignoreLayoutDuringTransition {
return
}
UIView.performWithoutAnimation({() -> Void in
self.frame.size = popupBarView.frame.size
self.layoutToolbar()
self.toolbar.layoutIfNeeded()
self.layoutAllViews()
if popupBarStyle == .custom {self.layoutCustomPopupBarView()}
})
}
/**
:nodoc:
*/
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
if #available(iOS 13.0, *) {
let style = self.traitCollection.userInterfaceStyle
self.highlightView.backgroundColor = style == .light ? UIColor.black.withAlphaComponent(0.1) : UIColor.white.withAlphaComponent(0.1)
}
}
// MARK: - Private Methods
internal func setHighlighted(_ highlighted: Bool, animated: Bool) {
let block = {
self.highlightView.alpha = highlighted ? 1.0 : 0.0
}
if animated {
UIView.animate(withDuration: 0.2, animations: block)
}
else {
UIView.performWithoutAnimation(block)
}
}
private func setupCustomPopupBarView() {
let hidden = self.popupBarStyle == .custom && self.customPopupBarViewController != nil
if self.popupBarStyle == .custom && self.customPopupBarViewController != nil {
self.addSubview(customPopupBarViewController!.view)
self.bringSubviewToFront(self.highlightView)
}
self.backgroundView.isHidden = hidden
self.toolbar.isHidden = hidden
self.safeAreaToolbar?.isHidden = hidden
self.titlesView.isHidden = hidden
}
private func layoutCustomPopupBarView() {
if self.customPopupBarViewController != nil {
self.customPopupBarViewController?.view.preservesSuperviewLayoutMargins = true
self.customPopupBarViewController?.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo:(customPopupBarViewController?.view.topAnchor)!),
self.leftAnchor.constraint(equalTo: (customPopupBarViewController?.view.leftAnchor)!),
self.rightAnchor.constraint(equalTo: (customPopupBarViewController?.view.rightAnchor)!),
self.bottomAnchor.constraint(equalTo: (customPopupBarViewController?.view.bottomAnchor)!)])
}