5
5
// Created by wangrui on 2017/4/19.
6
6
// Copyright © 2017年 wangrui. All rights reserved.
7
7
//
8
+ // Github地址:https://github.com/wangrui460/WRNavigationBar_swift
8
9
9
10
import UIKit
10
11
@@ -31,41 +32,19 @@ extension UINavigationBar
31
32
{
32
33
if ( backgroundView == nil )
33
34
{
34
- // 设置导航栏本身全透明
35
+ // add a image(nil color) to _UIBarBackground make it clear
35
36
setBackgroundImage ( UIImage ( ) , for: . default)
36
37
backgroundView = UIView ( frame: CGRect ( x: 0 , y: 0 , width: Int ( bounds. width) , height: 64 ) )
37
- // _UIBarBackground是导航栏的第一个子控件
38
+ // _UIBarBackground is first subView for navigationBar
38
39
subviews. first? . insertSubview ( backgroundView ?? UIView ( ) , at: 0 )
39
40
}
40
41
backgroundView? . backgroundColor = color
41
42
}
42
43
43
- // set navigationBar effectView alpha 设置导航栏毛玻璃的透明度
44
+ // set _UIBarBackground alpha (_UIBarBackground subviews alpha <= _UIBarBackground alpha)
44
45
fileprivate func wr_setBackgroundAlpha( alpha: CGFloat )
45
46
{
46
47
let barBackgroundView = subviews [ 0 ]
47
- let valueForKey = barBackgroundView. value ( forKey: )
48
-
49
- if let shadowView = valueForKey ( " _shadowView " ) as? UIView {
50
- shadowView. alpha = alpha
51
- }
52
-
53
- if isTranslucent {
54
- if #available( iOS 10 . 0 , * ) {
55
- if let backgroundEffectView = valueForKey ( " _backgroundEffectView " ) as? UIView , backgroundImage ( for: . default) == nil {
56
- backgroundEffectView. alpha = alpha
57
- return
58
- }
59
-
60
- } else {
61
- if let adaptiveBackdrop = valueForKey ( " _adaptiveBackdrop " ) as? UIView ,
62
- let backdropEffectView = adaptiveBackdrop. value ( forKey: " _backdropEffectView " ) as? UIView {
63
- backdropEffectView. alpha = alpha
64
- return
65
- }
66
- }
67
- }
68
-
69
48
barBackgroundView. alpha = alpha
70
49
}
71
50
@@ -76,10 +55,17 @@ extension UINavigationBar
76
55
{
77
56
if ( hasSystemBackIndicator == true )
78
57
{
79
- // _UIBarBackground对应的view是系统导航栏 ,不需要改变其透明度
58
+ // _UIBarBackground/_UINavigationBarBackground对应的view是系统导航栏 ,不需要改变其透明度
80
59
if let _UIBarBackgroundClass = NSClassFromString ( " _UIBarBackground " )
81
60
{
82
- if ( view. isKind ( of: _UIBarBackgroundClass) == false ) {
61
+ if view. isKind ( of: _UIBarBackgroundClass) == false {
62
+ view. alpha = alpha
63
+ }
64
+ }
65
+
66
+ if let _UINavigationBarBackground = NSClassFromString ( " _UINavigationBarBackground " )
67
+ {
68
+ if view. isKind ( of: _UINavigationBarBackground) == false {
83
69
view. alpha = alpha
84
70
}
85
71
}
@@ -88,10 +74,20 @@ extension UINavigationBar
88
74
{
89
75
// 这里如果不做判断的话,会显示 backIndicatorImage(系统返回按钮)
90
76
if let _UINavigationBarBackIndicatorViewClass = NSClassFromString ( " _UINavigationBarBackIndicatorView " ) ,
91
- let _UIBarBackgroundClass = NSClassFromString ( " _UIBarBackground " )
77
+ view . isKind ( of : _UINavigationBarBackIndicatorViewClass ) == false
92
78
{
93
- if ( view. isKind ( of: _UINavigationBarBackIndicatorViewClass) == false && view. isKind ( of: _UIBarBackgroundClass) == false ) {
94
- view. alpha = alpha
79
+ if let _UIBarBackgroundClass = NSClassFromString ( " _UIBarBackground " )
80
+ {
81
+ if view. isKind ( of: _UIBarBackgroundClass) == false {
82
+ view. alpha = alpha
83
+ }
84
+ }
85
+
86
+ if let _UINavigationBarBackground = NSClassFromString ( " _UINavigationBarBackground " )
87
+ {
88
+ if view. isKind ( of: _UINavigationBarBackground) == false {
89
+ view. alpha = alpha
90
+ }
95
91
}
96
92
}
97
93
}
@@ -103,15 +99,6 @@ extension UINavigationBar
103
99
{
104
100
transform = CGAffineTransform . init ( translationX: 0 , y: translationY)
105
101
}
106
-
107
- /// 清除在导航栏上设置的背景颜色、透明度、位移距离等属性
108
- func wr_clear( )
109
- {
110
- // 设置导航栏不透明
111
- setBackgroundImage ( nil , for: . default)
112
- backgroundView? . removeFromSuperview ( )
113
- backgroundView = nil
114
- }
115
102
}
116
103
117
104
//==========================================================================
@@ -128,9 +115,9 @@ extension UINavigationController
128
115
navigationBar. wr_setBackgroundColor ( color: barTintColor)
129
116
}
130
117
131
- fileprivate func setNeedsNavigationBarUpdate( effectAlpha : CGFloat )
118
+ fileprivate func setNeedsNavigationBarUpdate( barBackgroundAlpha : CGFloat )
132
119
{
133
- navigationBar. wr_setBackgroundAlpha ( alpha: effectAlpha )
120
+ navigationBar. wr_setBackgroundAlpha ( alpha: barBackgroundAlpha )
134
121
}
135
122
136
123
fileprivate func setNeedsNavigationBarUpdate( tintColor: UIColor ) {
@@ -235,11 +222,11 @@ extension UINavigationController
235
222
let newTintColor = UIColor . middleColor ( fromColor: fromTintColor, toColor: toTintColor, percent: popProgress)
236
223
setNeedsNavigationBarUpdate ( tintColor: newTintColor)
237
224
238
- // change navBar effectView alpha
239
- let fromEffectAlpha = fromVC? . navBarEffectAlpha ?? UIColor . defaultEffectAlpha
240
- let toEffectAlpha = toVC? . navBarEffectAlpha ?? UIColor . defaultEffectAlpha
241
- let newEffectAlpha = UIColor . middleAlpha ( fromAlpha: fromEffectAlpha , toAlpha: toEffectAlpha , percent: popProgress)
242
- setNeedsNavigationBarUpdate ( effectAlpha : newEffectAlpha )
225
+ // change navBar _UIBarBackground alpha
226
+ let fromBarBackgroundAlpha = fromVC? . navBarBackgroundAlpha ?? UIColor . defaultBackgroundAlpha
227
+ let toBarBackgroundAlpha = toVC? . navBarBackgroundAlpha ?? UIColor . defaultBackgroundAlpha
228
+ let newBarBackgroundAlpha = UIColor . middleAlpha ( fromAlpha: fromBarBackgroundAlpha , toAlpha: toBarBackgroundAlpha , percent: popProgress)
229
+ setNeedsNavigationBarUpdate ( barBackgroundAlpha : newBarBackgroundAlpha )
243
230
}
244
231
245
232
@@ -299,11 +286,11 @@ extension UINavigationController
299
286
let newTintColor = UIColor . middleColor ( fromColor: fromTintColor, toColor: toTintColor, percent: pushProgress)
300
287
setNeedsNavigationBarUpdate ( tintColor: newTintColor)
301
288
302
- // change navBar effectView alpha
303
- let fromEffectAlpha = fromVC? . navBarEffectAlpha ?? UIColor . defaultEffectAlpha
304
- let toEffectAlpha = toVC? . navBarEffectAlpha ?? UIColor . defaultEffectAlpha
305
- let newEffectAlpha = UIColor . middleAlpha ( fromAlpha: fromEffectAlpha , toAlpha: toEffectAlpha , percent: pushProgress)
306
- setNeedsNavigationBarUpdate ( effectAlpha : newEffectAlpha )
289
+ // change navBar _UIBarBackground alpha
290
+ let fromBarBackgroundAlpha = fromVC? . navBarBackgroundAlpha ?? UIColor . defaultBackgroundAlpha
291
+ let toBarBackgroundAlpha = toVC? . navBarBackgroundAlpha ?? UIColor . defaultBackgroundAlpha
292
+ let newBarBackgroundAlpha = UIColor . middleAlpha ( fromAlpha: fromBarBackgroundAlpha , toAlpha: toBarBackgroundAlpha , percent: pushProgress)
293
+ setNeedsNavigationBarUpdate ( barBackgroundAlpha : newBarBackgroundAlpha )
307
294
}
308
295
}
309
296
@@ -341,9 +328,10 @@ extension UINavigationController: UINavigationBarDelegate
341
328
{
342
329
let animations : ( UITransitionContextViewControllerKey ) -> ( ) = {
343
330
let curColor = context. viewController ( forKey: $0) ? . navBarBarTintColor ?? UIColor . defaultNavBarBarTintColor
344
- let curAlpha = context. viewController ( forKey: $0) ? . navBarEffectAlpha ?? UIColor . defaultEffectAlpha
331
+ let curAlpha = context. viewController ( forKey: $0) ? . navBarBackgroundAlpha ?? UIColor . defaultBackgroundAlpha
332
+
345
333
self . setNeedsNavigationBarUpdate ( barTintColor: curColor)
346
- self . setNeedsNavigationBarUpdate ( effectAlpha : curAlpha)
334
+ self . setNeedsNavigationBarUpdate ( barBackgroundAlpha : curAlpha)
347
335
}
348
336
349
337
// after that, cancel the gesture of return
@@ -388,11 +376,11 @@ extension UINavigationController: UINavigationBarDelegate
388
376
let newTintColor = UIColor . middleColor ( fromColor: fromTintColor, toColor: toTintColor, percent: percentComplete)
389
377
setNeedsNavigationBarUpdate ( tintColor: newTintColor)
390
378
391
- // change navBar effectView alpha
392
- let fromEffectAlpha = fromVC? . navBarEffectAlpha ?? UIColor . defaultEffectAlpha
393
- let toEffectAlpha = toVC? . navBarEffectAlpha ?? UIColor . defaultEffectAlpha
394
- let newEffectAlpha = UIColor . middleAlpha ( fromAlpha: fromEffectAlpha , toAlpha: toEffectAlpha , percent: percentComplete)
395
- setNeedsNavigationBarUpdate ( effectAlpha : newEffectAlpha )
379
+ // change navBar _UIBarBackground alpha
380
+ let fromBarBackgroundAlpha = fromVC? . navBarBackgroundAlpha ?? UIColor . defaultBackgroundAlpha
381
+ let toBarBackgroundAlpha = toVC? . navBarBackgroundAlpha ?? UIColor . defaultBackgroundAlpha
382
+ let newBarBackgroundAlpha = UIColor . middleAlpha ( fromAlpha: fromBarBackgroundAlpha , toAlpha: toBarBackgroundAlpha , percent: percentComplete)
383
+ setNeedsNavigationBarUpdate ( barBackgroundAlpha : newBarBackgroundAlpha )
396
384
397
385
wr_updateInteractiveTransition ( percentComplete)
398
386
}
@@ -407,10 +395,12 @@ extension UIViewController
407
395
{
408
396
static var pushToCurrentVCFinished : Bool = false
409
397
static var pushToNextVCFinished : Bool = false
398
+
410
399
static var navBarBarTintColor : UIColor = UIColor . defaultNavBarBarTintColor
411
- static var navBarEffectAlpha : CGFloat = 1.0
400
+ static var navBarBackgroundAlpha : CGFloat = 1.0
412
401
static var navBarTintColor : UIColor = UIColor . defaultNavBarTintColor
413
402
static var statusBarStyle : UIStatusBarStyle = UIStatusBarStyle . default
403
+
414
404
static var customNavBar : UINavigationBar = UINavigationBar ( )
415
405
}
416
406
@@ -464,16 +454,16 @@ extension UIViewController
464
454
}
465
455
}
466
456
467
- // navigationBar effectView alpha
468
- var navBarEffectAlpha : CGFloat {
457
+ // navigationBar _UIBarBackground alpha
458
+ var navBarBackgroundAlpha : CGFloat {
469
459
get {
470
- guard let effectAlpha = objc_getAssociatedObject ( self , & AssociatedKeys. navBarEffectAlpha ) as? CGFloat else {
460
+ guard let barBackgroundAlpha = objc_getAssociatedObject ( self , & AssociatedKeys. navBarBackgroundAlpha ) as? CGFloat else {
471
461
return 1.0
472
462
}
473
- return effectAlpha
463
+ return barBackgroundAlpha
474
464
}
475
465
set {
476
- objc_setAssociatedObject ( self , & AssociatedKeys. navBarEffectAlpha , newValue, . OBJC_ASSOCIATION_RETAIN_NONATOMIC)
466
+ objc_setAssociatedObject ( self , & AssociatedKeys. navBarBackgroundAlpha , newValue, . OBJC_ASSOCIATION_RETAIN_NONATOMIC)
477
467
478
468
if customNavBar. isKind ( of: UINavigationBar . self) {
479
469
let navBar = customNavBar as! UINavigationBar
@@ -482,7 +472,7 @@ extension UIViewController
482
472
else
483
473
{
484
474
if pushToCurrentVCFinished == true && pushToNextVCFinished == false {
485
- navigationController? . setNeedsNavigationBarUpdate ( effectAlpha : newValue)
475
+ navigationController? . setNeedsNavigationBarUpdate ( barBackgroundAlpha : newValue)
486
476
}
487
477
}
488
478
}
@@ -582,7 +572,7 @@ extension UIViewController
582
572
func wr_viewDidAppear( _ animated: Bool )
583
573
{
584
574
navigationController? . setNeedsNavigationBarUpdate ( barTintColor: navBarBarTintColor)
585
- navigationController? . setNeedsNavigationBarUpdate ( effectAlpha : navBarEffectAlpha )
575
+ navigationController? . setNeedsNavigationBarUpdate ( barBackgroundAlpha : navBarBackgroundAlpha )
586
576
navigationController? . setNeedsNavigationBarUpdate ( tintColor: navBarTintColor)
587
577
wr_viewDidAppear ( animated)
588
578
}
@@ -619,9 +609,9 @@ extension DispatchQueue {
619
609
extension UIColor
620
610
{
621
611
fileprivate struct AssociatedKeys
622
- {
623
- static var defNavBarBarTintColor : UIColor = UIColor . white // UIColor.init(red: 0/255.0, green: 175/255.0, blue: 240/255.0, alpha: 1)
624
- static var defNavBarTintColor : UIColor = UIColor . white
612
+ { // default is system attributes
613
+ static var defNavBarBarTintColor : UIColor = UIColor . white
614
+ static var defNavBarTintColor : UIColor = UIColor ( red : 0 , green : 0.478431 , blue : 1 , alpha : 1.0 )
625
615
static var defStatusBarStyle : UIStatusBarStyle = UIStatusBarStyle . default
626
616
}
627
617
class var defaultNavBarBarTintColor : UIColor {
@@ -660,7 +650,7 @@ extension UIColor
660
650
}
661
651
}
662
652
663
- class var defaultEffectAlpha : CGFloat {
653
+ class var defaultBackgroundAlpha : CGFloat {
664
654
get {
665
655
return 1.0
666
656
}
@@ -684,11 +674,11 @@ extension UIColor
684
674
toColor. getRed ( & toRed, green: & toGreen, blue: & toBlue, alpha: & toAlpha)
685
675
686
676
// calculate middle color RGBA
687
- let nowRed = fromRed + ( toRed - fromRed) * percent
688
- let nowGreen = fromGreen + ( toGreen - fromGreen) * percent
689
- let nowBlue = fromBlue + ( toBlue - fromBlue) * percent
690
- let nowAlpha = fromAlpha + ( toAlpha - fromAlpha) * percent
691
- return UIColor ( red: nowRed , green: nowGreen , blue: nowBlue , alpha: nowAlpha )
677
+ let newRed = fromRed + ( toRed - fromRed) * percent
678
+ let newGreen = fromGreen + ( toGreen - fromGreen) * percent
679
+ let newBlue = fromBlue + ( toBlue - fromBlue) * percent
680
+ let newAlpha = fromAlpha + ( toAlpha - fromAlpha) * percent
681
+ return UIColor ( red: newRed , green: newGreen , blue: newBlue , alpha: newAlpha )
692
682
}
693
683
694
684
// Calculate the middle alpha
0 commit comments