|
15 | 15 |
|
16 | 16 | #import "UINavigationBar+Transition.h" |
17 | 17 | #import "QMUICore.h" |
| 18 | +#import "UINavigationBar+QMUI.h" |
| 19 | +#import "QMUIWeakObjectContainer.h" |
| 20 | +#import "UIImage+QMUI.h" |
18 | 21 |
|
19 | 22 | @implementation UINavigationBar (Transition) |
20 | 23 |
|
21 | | -QMUISynthesizeIdStrongProperty(transitionNavigationBar, setTransitionNavigationBar) |
22 | | - |
23 | 24 | + (void)load { |
24 | 25 | static dispatch_once_t onceToken; |
25 | 26 | dispatch_once(&onceToken, ^{ |
26 | | - ExtendImplementationOfVoidMethodWithSingleArgument([UINavigationBar class], @selector(setShadowImage:), UIImage *, ^(UINavigationBar *selfObject, UIImage *firstArgv) { |
27 | | - if (selfObject.transitionNavigationBar) { |
28 | | - selfObject.transitionNavigationBar.shadowImage = firstArgv; |
| 27 | + |
| 28 | +#ifdef IOS15_SDK_ALLOWED |
| 29 | + if (@available(iOS 15.0, *)) { |
| 30 | + ExtendImplementationOfVoidMethodWithSingleArgument([UINavigationBar class], @selector(setStandardAppearance:), UINavigationBarAppearance *, ^(UINavigationBar *selfObject, UINavigationBarAppearance *appearance) { |
| 31 | + if (selfObject.qmuinb_copyStylesToBar) { |
| 32 | + selfObject.qmuinb_copyStylesToBar.standardAppearance = appearance; |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + ExtendImplementationOfVoidMethodWithSingleArgument([UINavigationBar class], @selector(setScrollEdgeAppearance:), UINavigationBarAppearance *, ^(UINavigationBar *selfObject, UINavigationBarAppearance *appearance) { |
| 37 | + if (selfObject.qmuinb_copyStylesToBar) { |
| 38 | + selfObject.qmuinb_copyStylesToBar.scrollEdgeAppearance = appearance; |
| 39 | + } |
| 40 | + }); |
| 41 | + } |
| 42 | +#endif |
| 43 | + |
| 44 | + ExtendImplementationOfVoidMethodWithSingleArgument([UINavigationBar class], @selector(setBarStyle:), UIBarStyle, ^(UINavigationBar *selfObject, UIBarStyle barStyle) { |
| 45 | + if (selfObject.qmuinb_copyStylesToBar && selfObject.qmuinb_copyStylesToBar.barStyle != barStyle) { |
| 46 | + selfObject.qmuinb_copyStylesToBar.barStyle = barStyle; |
29 | 47 | } |
30 | 48 | }); |
31 | 49 |
|
32 | | - ExtendImplementationOfVoidMethodWithSingleArgument([UINavigationBar class], @selector(setBarTintColor:), UIColor *, ^(UINavigationBar *selfObject, UIColor *firstArgv) { |
33 | | - if (selfObject.transitionNavigationBar) { |
34 | | - selfObject.transitionNavigationBar.barTintColor = firstArgv; |
| 50 | + ExtendImplementationOfVoidMethodWithSingleArgument([UINavigationBar class], @selector(setBarTintColor:), UIColor *, ^(UINavigationBar *selfObject, UIColor *barTintColor) { |
| 51 | + if (selfObject.qmuinb_copyStylesToBar && ![selfObject.qmuinb_copyStylesToBar.barTintColor isEqual:barTintColor]) { |
| 52 | + selfObject.qmuinb_copyStylesToBar.barTintColor = barTintColor; |
35 | 53 | } |
36 | 54 | }); |
37 | 55 |
|
38 | 56 | ExtendImplementationOfVoidMethodWithTwoArguments([UINavigationBar class], @selector(setBackgroundImage:forBarMetrics:), UIImage *, UIBarMetrics, ^(UINavigationBar *selfObject, UIImage *backgroundImage, UIBarMetrics barMetrics) { |
39 | | - if (selfObject.transitionNavigationBar) { |
40 | | - [selfObject.transitionNavigationBar setBackgroundImage:backgroundImage forBarMetrics:barMetrics]; |
| 57 | + if (selfObject.qmuinb_copyStylesToBar) { |
| 58 | + [selfObject.qmuinb_copyStylesToBar setBackgroundImage:backgroundImage forBarMetrics:barMetrics]; |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + ExtendImplementationOfVoidMethodWithSingleArgument([UINavigationBar class], @selector(setShadowImage:), UIImage *, ^(UINavigationBar *selfObject, UIImage *firstArgv) { |
| 63 | + if (selfObject.qmuinb_copyStylesToBar) { |
| 64 | + selfObject.qmuinb_copyStylesToBar.shadowImage = firstArgv; |
41 | 65 | } |
42 | 66 | }); |
43 | 67 | }); |
44 | 68 | } |
45 | 69 |
|
| 70 | +static char kAssociatedObjectKey_copyStylesToBar; |
| 71 | +- (void)setQmuinb_copyStylesToBar:(UINavigationBar *)copyStylesToBar { |
| 72 | + QMUIWeakObjectContainer *weakContainer = objc_getAssociatedObject(self, &kAssociatedObjectKey_copyStylesToBar); |
| 73 | + if (!weakContainer) { |
| 74 | + weakContainer = [[QMUIWeakObjectContainer alloc] init]; |
| 75 | + } |
| 76 | + weakContainer.object = copyStylesToBar; |
| 77 | + objc_setAssociatedObject(self, &kAssociatedObjectKey_copyStylesToBar, weakContainer, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 78 | + |
| 79 | +#ifdef IOS15_SDK_ALLOWED |
| 80 | + if (@available(iOS 15.0, *)) { |
| 81 | + copyStylesToBar.standardAppearance = self.standardAppearance; |
| 82 | + copyStylesToBar.scrollEdgeAppearance = self.scrollEdgeAppearance; |
| 83 | + } else { |
| 84 | +#endif |
| 85 | + UIImage *backgroundImage = [self backgroundImageForBarMetrics:UIBarMetricsDefault]; |
| 86 | + if (backgroundImage && backgroundImage.size.width <= 0 && backgroundImage.size.height <= 0) { |
| 87 | + // 假设这里的图片时通过`[UIImage new]`这种形式创建的,那么会navBar会奇怪地显示为系统默认navBar的样式。不知道为什么 navController 设置自己的 navBar 为 [UIImage new] 却没事,所以这里做个保护。 |
| 88 | + backgroundImage = [UIImage qmui_imageWithColor:UIColorClear]; |
| 89 | + } |
| 90 | + [copyStylesToBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; |
| 91 | + |
| 92 | + copyStylesToBar.shadowImage = self.shadowImage; |
| 93 | + |
| 94 | + if (copyStylesToBar.barStyle != self.barStyle) { |
| 95 | + copyStylesToBar.barStyle = self.barStyle; |
| 96 | + } |
| 97 | + |
| 98 | + // setTranslucent 要在 setBackgroundImage 之后,因为 setBackgroundImage 内部会改变 translucent 的值 |
| 99 | + if (copyStylesToBar.translucent != self.translucent) { |
| 100 | + copyStylesToBar.translucent = self.translucent; |
| 101 | + } |
| 102 | + |
| 103 | + if (![copyStylesToBar.barTintColor isEqual:self.barTintColor]) { |
| 104 | + copyStylesToBar.barTintColor = self.barTintColor; |
| 105 | + } |
| 106 | +#ifdef IOS15_SDK_ALLOWED |
| 107 | + } |
| 108 | +#endif |
| 109 | +} |
| 110 | + |
| 111 | +- (UINavigationBar *)qmuinb_copyStylesToBar { |
| 112 | + return (UINavigationBar *)((QMUIWeakObjectContainer *)objc_getAssociatedObject(self, &kAssociatedObjectKey_copyStylesToBar)).object; |
| 113 | +} |
| 114 | + |
| 115 | +@end |
| 116 | + |
| 117 | +@implementation _QMUITransitionNavigationBar |
| 118 | + |
| 119 | ++ (void)load { |
| 120 | + static dispatch_once_t onceToken; |
| 121 | + dispatch_once(&onceToken, ^{ |
| 122 | + // iOS 14 开启 customNavigationBarTransitionKey 的情况下转场效果错误 |
| 123 | + // https://github.com/Tencent/QMUI_iOS/issues/1081 |
| 124 | + if (@available(iOS 14.0, *)) { |
| 125 | + OverrideImplementation([_QMUITransitionNavigationBar class], NSSelectorFromString([NSString stringWithFormat:@"_%@_%@", @"accessibility", @"navigationController"]), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) { |
| 126 | + return ^UINavigationController *(_QMUITransitionNavigationBar *selfObject) { |
| 127 | + if (selfObject.originalNavigationBar) { |
| 128 | + BeginIgnorePerformSelectorLeaksWarning |
| 129 | + return [selfObject.originalNavigationBar performSelector:originCMD]; |
| 130 | + EndIgnorePerformSelectorLeaksWarning |
| 131 | + } |
| 132 | + |
| 133 | + // call super |
| 134 | + UINavigationController *(*originSelectorIMP)(id, SEL); |
| 135 | + originSelectorIMP = (UINavigationController *(*)(id, SEL))originalIMPProvider(); |
| 136 | + UINavigationController *result = originSelectorIMP(selfObject, originCMD); |
| 137 | + return result; |
| 138 | + }; |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | +#ifdef IOS15_SDK_ALLOWED |
| 143 | + if (@available(iOS 15.0, *)) { |
| 144 | + // -[UINavigationBar _didMoveFromWindow:toWindow:] |
| 145 | + OverrideImplementation([_QMUITransitionNavigationBar class], NSSelectorFromString(@"_didMoveFromWindow:toWindow:"), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) { |
| 146 | + return ^(_QMUITransitionNavigationBar *selfObject, UIWindow *firstArgv, UIWindow *secondArgv) { |
| 147 | + |
| 148 | + if (selfObject.shouldPreventAppearance) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + // call super |
| 153 | + void (*originSelectorIMP)(id, SEL, UIWindow *, UIWindow *); |
| 154 | + originSelectorIMP = (void (*)(id, SEL, UIWindow *, UIWindow *))originalIMPProvider(); |
| 155 | + originSelectorIMP(selfObject, originCMD, firstArgv, secondArgv); |
| 156 | + }; |
| 157 | + }); |
| 158 | + } |
| 159 | +#endif |
| 160 | + |
| 161 | + }); |
| 162 | +} |
| 163 | + |
| 164 | +- (void)setOriginalNavigationBar:(UINavigationBar *)originBar { |
| 165 | + _originalNavigationBar = originBar; |
| 166 | + |
| 167 | + // 只复制当前 originBar 的样式,所以复制完立马就清空 |
| 168 | + originBar.qmuinb_copyStylesToBar = self; |
| 169 | + originBar.qmuinb_copyStylesToBar = nil; |
| 170 | +} |
| 171 | + |
| 172 | +- (void)layoutSubviews { |
| 173 | + [super layoutSubviews]; |
| 174 | + // 实测 iOS 11 Beta 1-5 里,自己 init 的 navigationBar.backgroundView.height 默认一直是 44,所以才加上这个兼容 |
| 175 | + self.qmui_backgroundView.frame = self.bounds; |
| 176 | +} |
| 177 | + |
46 | 178 | @end |
0 commit comments