Browse Source

Fix topBar.background.color on iOS 13 (#5685)

Yogev Ben David 5 years ago
parent
commit
6f13d6963c

+ 2
- 31
lib/ios/RNNStackController.m View File

@@ -1,7 +1,6 @@
1 1
 #import "RNNStackController.h"
2 2
 #import "RNNComponentViewController.h"
3
-
4
-const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
3
+#import "UINavigationBar+utils.h"
5 4
 
6 5
 @implementation RNNStackController
7 6
 
@@ -52,35 +51,7 @@ const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
52 51
 }
53 52
 
54 53
 - (void)setTopBarBackgroundColor:(UIColor *)backgroundColor {
55
-	if (backgroundColor) {
56
-		CGFloat bgColorAlpha = CGColorGetAlpha(backgroundColor.CGColor);
57
-		
58
-		if (bgColorAlpha == 0.0) {
59
-			if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
60
-				UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
61
-				transparentView.backgroundColor = [UIColor clearColor];
62
-				transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
63
-				[self.navigationBar insertSubview:transparentView atIndex:0];
64
-			}
65
-			self.navigationBar.translucent = YES;
66
-			[self.navigationBar setBackgroundColor:[UIColor clearColor]];
67
-			self.navigationBar.shadowImage = [UIImage new];
68
-			[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
69
-		} else {
70
-			self.navigationBar.barTintColor = backgroundColor;
71
-			UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
72
-			if (transparentView){
73
-				[transparentView removeFromSuperview];
74
-			}
75
-		}
76
-	} else {
77
-		UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
78
-		if (transparentView){
79
-			[transparentView removeFromSuperview];
80
-		}
81
-		
82
-		self.navigationBar.barTintColor = nil;
83
-	}
54
+	[self.navigationBar rnn_setBackgroundColor:backgroundColor];
84 55
 }
85 56
 
86 57
 @end

+ 8
- 0
lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj View File

@@ -208,6 +208,8 @@
208 208
 		509B258F2178BE7A00C83C23 /* RNNStackPresenterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 509B258E2178BE7A00C83C23 /* RNNStackPresenterTest.m */; };
209 209
 		50A00C37200F84D6000F01A6 /* RNNOverlayOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 50A00C35200F84D6000F01A6 /* RNNOverlayOptions.h */; };
210 210
 		50A00C38200F84D6000F01A6 /* RNNOverlayOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A00C36200F84D6000F01A6 /* RNNOverlayOptions.m */; };
211
+		50A409CA238D444900D5FF7D /* UINavigationBar+utils.h in Headers */ = {isa = PBXBuildFile; fileRef = 50A409C8238D444900D5FF7D /* UINavigationBar+utils.h */; };
212
+		50A409CB238D444900D5FF7D /* UINavigationBar+utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A409C9238D444900D5FF7D /* UINavigationBar+utils.m */; };
211 213
 		50AB0B1C2255F8640039DAED /* UIViewController+LayoutProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 50AB0B1A2255F8640039DAED /* UIViewController+LayoutProtocol.h */; };
212 214
 		50AB0B1D2255F8640039DAED /* UIViewController+LayoutProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = 50AB0B1B2255F8640039DAED /* UIViewController+LayoutProtocol.m */; };
213 215
 		50AB0B1F22562FA10039DAED /* UIViewController+LayoutProtocolTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 50AB0B1E22562FA10039DAED /* UIViewController+LayoutProtocolTest.m */; };
@@ -558,6 +560,8 @@
558 560
 		509B258E2178BE7A00C83C23 /* RNNStackPresenterTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNStackPresenterTest.m; sourceTree = "<group>"; };
559 561
 		50A00C35200F84D6000F01A6 /* RNNOverlayOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNOverlayOptions.h; sourceTree = "<group>"; };
560 562
 		50A00C36200F84D6000F01A6 /* RNNOverlayOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNOverlayOptions.m; sourceTree = "<group>"; };
563
+		50A409C8238D444900D5FF7D /* UINavigationBar+utils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UINavigationBar+utils.h"; sourceTree = "<group>"; };
564
+		50A409C9238D444900D5FF7D /* UINavigationBar+utils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UINavigationBar+utils.m"; sourceTree = "<group>"; };
561 565
 		50AB0B1A2255F8640039DAED /* UIViewController+LayoutProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIViewController+LayoutProtocol.h"; sourceTree = "<group>"; };
562 566
 		50AB0B1B2255F8640039DAED /* UIViewController+LayoutProtocol.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+LayoutProtocol.m"; sourceTree = "<group>"; };
563 567
 		50AB0B1E22562FA10039DAED /* UIViewController+LayoutProtocolTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+LayoutProtocolTest.m"; sourceTree = "<group>"; };
@@ -1173,6 +1177,8 @@
1173 1177
 				E5F6C39F22DB4D0E0093C2CE /* UIView+Utils.m */,
1174 1178
 				E5F6C39D22DB4D0E0093C2CE /* UIViewController+Utils.h */,
1175 1179
 				E5F6C39E22DB4D0E0093C2CE /* UIViewController+Utils.m */,
1180
+				50A409C8238D444900D5FF7D /* UINavigationBar+utils.h */,
1181
+				50A409C9238D444900D5FF7D /* UINavigationBar+utils.m */,
1176 1182
 			);
1177 1183
 			name = Utils;
1178 1184
 			sourceTree = "<group>";
@@ -1326,6 +1332,7 @@
1326 1332
 				E8E518321F83B3E0000467AC /* RNNUtils.h in Headers */,
1327 1333
 				50E5F791223FA04C002AFEAD /* RNNAnimationConfigurationOptions.h in Headers */,
1328 1334
 				5049594E216F6277006D2B81 /* NumberParser.h in Headers */,
1335
+				50A409CA238D444900D5FF7D /* UINavigationBar+utils.h in Headers */,
1329 1336
 				50BE951320B5A787004F5DF5 /* RNNStatusBarOptions.h in Headers */,
1330 1337
 				50570BEA2063E09B006A1B5C /* RNNTitleViewHelper.h in Headers */,
1331 1338
 				50495956216F6B3D006D2B81 /* DictionaryParser.h in Headers */,
@@ -1512,6 +1519,7 @@
1512 1519
 			buildActionMask = 2147483647;
1513 1520
 			files = (
1514 1521
 				50C4A497206BDDBB00DB292E /* RNNSubtitleOptions.m in Sources */,
1522
+				50A409CB238D444900D5FF7D /* UINavigationBar+utils.m in Sources */,
1515 1523
 				50AB0B1D2255F8640039DAED /* UIViewController+LayoutProtocol.m in Sources */,
1516 1524
 				263905B41E4C6F440023D7D3 /* MMDrawerVisualState.m in Sources */,
1517 1525
 				5012240B21735959000F5F98 /* RNNSideMenuPresenter.m in Sources */,

+ 9
- 0
lib/ios/UINavigationBar+utils.h View File

@@ -0,0 +1,9 @@
1
+#import <UIKit/UIKit.h>
2
+
3
+@interface UINavigationBar (utils)
4
+
5
+- (void)rnn_setBackgroundColor:(UIColor *)color;
6
+
7
+- (void)rnn_setBackgroundColorTransparent;
8
+
9
+@end

+ 82
- 0
lib/ios/UINavigationBar+utils.m View File

@@ -0,0 +1,82 @@
1
+#import "UINavigationBar+utils.h"
2
+
3
+const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
4
+
5
+@implementation UINavigationBar (utils)
6
+
7
+- (void)rnn_setBackgroundColor:(UIColor *)color {
8
+    CGFloat bgColorAlpha = CGColorGetAlpha(color.CGColor);
9
+    
10
+    if (color && bgColorAlpha == 0.0) {
11
+        [self rnn_setBackgroundColorTransparent];
12
+    } else {
13
+        [self removeTransparentView];
14
+        if (@available(iOS 13.0, *)) {
15
+            [self getNavigaitonBarStandardAppearance].backgroundColor = color;
16
+            [self getNavigaitonBarCompactAppearance].backgroundColor = color;
17
+            [self getNavigaitonBarScrollEdgeAppearance].backgroundColor = color;
18
+        }
19
+        self.barTintColor = color;
20
+    }
21
+}
22
+
23
+- (void)rnn_setBackgroundColorTransparent {
24
+    if (![self viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
25
+        UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
26
+        transparentView.backgroundColor = [UIColor clearColor];
27
+        transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
28
+        [self insertSubview:transparentView atIndex:0];
29
+    }
30
+    
31
+    self.translucent = YES;
32
+    [self setBackgroundColor:[UIColor clearColor]];
33
+    self.shadowImage = [UIImage new];
34
+    [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
35
+    if (@available(iOS 13.0, *)) {
36
+        UINavigationBarAppearance *standardAppearance = [self getNavigaitonBarStandardAppearance];
37
+        standardAppearance.backgroundColor = [UIColor clearColor];
38
+        standardAppearance.shadowImage =  [UIImage new];
39
+        standardAppearance.backgroundImage = [UIImage new];
40
+
41
+        UINavigationBarAppearance *compactAppearance = [self getNavigaitonBarCompactAppearance];
42
+        compactAppearance.backgroundColor = [UIColor clearColor];
43
+        compactAppearance.shadowImage =  [UIImage new];
44
+        compactAppearance.backgroundImage = [UIImage new];
45
+
46
+        UINavigationBarAppearance *scrollEdgeAppearance = [self getNavigaitonBarScrollEdgeAppearance];
47
+        scrollEdgeAppearance.backgroundColor = [UIColor clearColor];
48
+        scrollEdgeAppearance.shadowImage =  [UIImage new];
49
+        scrollEdgeAppearance.backgroundImage = [UIImage new];
50
+    }
51
+}
52
+
53
+- (void)removeTransparentView {
54
+    UIView *transparentView = [self viewWithTag:TOP_BAR_TRANSPARENT_TAG];
55
+    if (transparentView){
56
+        [transparentView removeFromSuperview];
57
+    }
58
+}
59
+
60
+
61
+- (UINavigationBarAppearance*)getNavigaitonBarStandardAppearance  API_AVAILABLE(ios(13.0)) {
62
+    if (!self.standardAppearance) {
63
+        self.standardAppearance = [UINavigationBarAppearance new];
64
+    }
65
+    return self.standardAppearance;
66
+}
67
+
68
+- (UINavigationBarAppearance*)getNavigaitonBarCompactAppearance  API_AVAILABLE(ios(13.0)) {
69
+    if (!self.compactAppearance) {
70
+        self.compactAppearance = [UINavigationBarAppearance new];
71
+    }
72
+    return self.compactAppearance;
73
+}
74
+
75
+- (UINavigationBarAppearance*)getNavigaitonBarScrollEdgeAppearance  API_AVAILABLE(ios(13.0)) {
76
+    if (!self.scrollEdgeAppearance) {
77
+        self.scrollEdgeAppearance = [UINavigationBarAppearance new];
78
+    }
79
+    return self.scrollEdgeAppearance;
80
+}
81
+
82
+@end

+ 9
- 0
playground/ios/NavigationTests/RNNNavigationControllerTest.m View File

@@ -156,6 +156,9 @@
156 156
 	UIColor* color = UIColor.redColor;
157 157
 	[self.uut setTopBarBackgroundColor:color];
158 158
 	XCTAssertEqual(self.uut.navigationBar.barTintColor, color);
159
+	XCTAssertEqual(self.uut.navigationBar.standardAppearance.backgroundColor, color);
160
+	XCTAssertEqual(self.uut.navigationBar.compactAppearance.backgroundColor, color);
161
+	XCTAssertEqual(self.uut.navigationBar.scrollEdgeAppearance.backgroundColor, color);
159 162
 }
160 163
 
161 164
 - (void)testSetTopBarBackgroundColor_ShouldSetTransparentBackgroundColor {
@@ -166,6 +169,9 @@
166 169
 	XCTAssertTrue(self.uut.navigationBar.translucent);
167 170
 	XCTAssertNotNil(self.uut.navigationBar.shadowImage);
168 171
 	XCTAssertNotNil([self.uut.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault]);
172
+	XCTAssertNil(self.uut.navigationBar.standardAppearance.backgroundColor);
173
+	XCTAssertNil(self.uut.navigationBar.compactAppearance.backgroundColor);
174
+	XCTAssertNil(self.uut.navigationBar.scrollEdgeAppearance.backgroundColor);
169 175
 }
170 176
 
171 177
 - (void)testSetTopBarBackgroundColor_ShouldRemoveTransparentView {
@@ -188,6 +194,9 @@
188 194
 	
189 195
 	XCTAssertNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
190 196
 	XCTAssertNil(self.uut.navigationBar.barTintColor);
197
+	XCTAssertNil(self.uut.navigationBar.standardAppearance.backgroundColor);
198
+	XCTAssertNil(self.uut.navigationBar.compactAppearance.backgroundColor);
199
+	XCTAssertNil(self.uut.navigationBar.scrollEdgeAppearance.backgroundColor);
191 200
 }
192 201
 
193 202
 - (RNNStackController *)createNavigationControllerWithOptions:(RNNNavigationOptions *)options {