Ver código fonte

Fix transparent topBar background color transition (#5317)

Yogev Ben David 5 anos atrás
pai
commit
147cf4cafc
Nenhuma conta vinculada ao e-mail do autor do commit

+ 1
- 28
lib/ios/RNNNavigationController.m Ver arquivo

@@ -4,12 +4,6 @@
4 4
 
5 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 7
 @implementation RNNNavigationController
14 8
 
15 9
 - (void)viewDidLayoutSubviews {
@@ -22,7 +16,7 @@ const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
22 16
 }
23 17
 
24 18
 - (CGFloat)getTopBarHeight {
25
-    return self.navigationBar.frame.size.height;
19
+	return self.navigationBar.frame.size.height;
26 20
 }
27 21
 
28 22
 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
@@ -63,7 +57,6 @@ const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
63 57
 		
64 58
 		if (bgColorAlpha == 0.0) {
65 59
 			if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
66
-				[self storeOriginalTopBarImages:self];
67 60
 				UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
68 61
 				transparentView.backgroundColor = [UIColor clearColor];
69 62
 				transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
@@ -78,36 +71,16 @@ const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
78 71
 			UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
79 72
 			if (transparentView){
80 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 76
 	} else {
87 77
 		UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
88 78
 		if (transparentView){
89 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 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 86
 @end

+ 38
- 0
lib/ios/ReactNativeNavigationTests/RNNNavigationControllerTest.m Ver arquivo

@@ -152,6 +152,44 @@
152 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 193
 - (RNNNavigationController *)createNavigationControllerWithOptions:(RNNNavigationOptions *)options {
156 194
 	RNNNavigationController* nav = [[RNNNavigationController alloc] initWithLayoutInfo:nil creator:_creator options:options defaultOptions:nil presenter:[[RNNNavigationControllerPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1]];
157 195
 	return nav;