Browse Source

Fix transparent topBar background color transition (#5317)

Yogev Ben David 5 years ago
parent
commit
147cf4cafc
No account linked to committer's email address

+ 1
- 28
lib/ios/RNNNavigationController.m View File

4
 
4
 
5
 const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
5
 const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
6
 
6
 
7
-@interface RNNNavigationController()
8
-
9
-@property (nonatomic, strong) NSMutableDictionary* originalTopBarImages;
10
-
11
-@end
12
-
13
 @implementation RNNNavigationController
7
 @implementation RNNNavigationController
14
 
8
 
15
 - (void)viewDidLayoutSubviews {
9
 - (void)viewDidLayoutSubviews {
22
 }
16
 }
23
 
17
 
24
 - (CGFloat)getTopBarHeight {
18
 - (CGFloat)getTopBarHeight {
25
-    return self.navigationBar.frame.size.height;
19
+	return self.navigationBar.frame.size.height;
26
 }
20
 }
27
 
21
 
28
 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
22
 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
63
 		
57
 		
64
 		if (bgColorAlpha == 0.0) {
58
 		if (bgColorAlpha == 0.0) {
65
 			if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
59
 			if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
66
-				[self storeOriginalTopBarImages:self];
67
 				UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
60
 				UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
68
 				transparentView.backgroundColor = [UIColor clearColor];
61
 				transparentView.backgroundColor = [UIColor clearColor];
69
 				transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
62
 				transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
78
 			UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
71
 			UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
79
 			if (transparentView){
72
 			if (transparentView){
80
 				[transparentView removeFromSuperview];
73
 				[transparentView removeFromSuperview];
81
-				[self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
82
-				self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
83
-				self.originalTopBarImages = nil;
84
 			}
74
 			}
85
 		}
75
 		}
86
 	} else {
76
 	} else {
87
 		UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
77
 		UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
88
 		if (transparentView){
78
 		if (transparentView){
89
 			[transparentView removeFromSuperview];
79
 			[transparentView removeFromSuperview];
90
-			[self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] ? self.originalTopBarImages[@"backgroundImage"] : [self.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
91
-			self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"] ? self.originalTopBarImages[@"shadowImage"] : self.navigationBar.shadowImage;
92
-			self.originalTopBarImages = nil;
93
 		}
80
 		}
94
 		
81
 		
95
 		self.navigationBar.barTintColor = nil;
82
 		self.navigationBar.barTintColor = nil;
96
 	}
83
 	}
97
 }
84
 }
98
 
85
 
99
-- (void)storeOriginalTopBarImages:(UINavigationController *)navigationController {
100
-	NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
101
-	UIImage *bgImage = [navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
102
-	if (bgImage != nil) {
103
-		originalTopBarImages[@"backgroundImage"] = bgImage;
104
-	}
105
-	UIImage *shadowImage = navigationController.navigationBar.shadowImage;
106
-	if (shadowImage != nil) {
107
-		originalTopBarImages[@"shadowImage"] = shadowImage;
108
-	}
109
-	self.originalTopBarImages = originalTopBarImages;
110
-}
111
-
112
-
113
 @end
86
 @end

+ 38
- 0
lib/ios/ReactNativeNavigationTests/RNNNavigationControllerTest.m View File

152
 	[(id)self.uut.options verify];
152
 	[(id)self.uut.options verify];
153
 }
153
 }
154
 
154
 
155
+- (void)testSetTopBarBackgroundColor_ShouldSetBackgroundColor {
156
+	UIColor* color = UIColor.redColor;
157
+	[self.uut setTopBarBackgroundColor:color];
158
+	XCTAssertEqual(self.uut.navigationBar.barTintColor, color);
159
+}
160
+
161
+- (void)testSetTopBarBackgroundColor_ShouldSetTransparentBackgroundColor {
162
+	UIColor* transparentColor = UIColor.clearColor;
163
+	[self.uut setTopBarBackgroundColor:transparentColor];
164
+
165
+	XCTAssertEqual(self.uut.navigationBar.backgroundColor, transparentColor);
166
+	XCTAssertTrue(self.uut.navigationBar.translucent);
167
+	XCTAssertNotNil(self.uut.navigationBar.shadowImage);
168
+	XCTAssertNotNil([self.uut.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault]);
169
+}
170
+
171
+- (void)testSetTopBarBackgroundColor_ShouldRemoveTransparentView {
172
+	UIColor* transparentColor = UIColor.clearColor;
173
+	UIColor* redColor = UIColor.redColor;
174
+	
175
+	[self.uut setTopBarBackgroundColor:transparentColor];
176
+	XCTAssertNotNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
177
+	[self.uut setTopBarBackgroundColor:redColor];
178
+	XCTAssertNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
179
+}
180
+
181
+- (void)testSetTopBarBackgroundColor_NilColorShouldResetNavigationBar {
182
+	UIColor* transparentColor = UIColor.clearColor;
183
+	UIColor* redColor = UIColor.redColor;
184
+	
185
+	[self.uut setTopBarBackgroundColor:transparentColor];
186
+	[self.uut setTopBarBackgroundColor:redColor];
187
+	[self.uut setTopBarBackgroundColor:nil];
188
+	
189
+	XCTAssertNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
190
+	XCTAssertNil(self.uut.navigationBar.barTintColor);
191
+}
192
+
155
 - (RNNNavigationController *)createNavigationControllerWithOptions:(RNNNavigationOptions *)options {
193
 - (RNNNavigationController *)createNavigationControllerWithOptions:(RNNNavigationOptions *)options {
156
 	RNNNavigationController* nav = [[RNNNavigationController alloc] initWithLayoutInfo:nil creator:_creator options:options defaultOptions:nil presenter:[[RNNNavigationControllerPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1]];
194
 	RNNNavigationController* nav = [[RNNNavigationController alloc] initWithLayoutInfo:nil creator:_creator options:options defaultOptions:nil presenter:[[RNNNavigationControllerPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1]];
157
 	return nav;
195
 	return nav;