Преглед на файлове

Fix transparent topBar option (#5704)

Yogev Ben David преди 5 години
родител
ревизия
b56026553d
No account linked to committer's email address

+ 51
- 40
lib/ios/UINavigationBar+utils.m Целия файл

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

+ 1
- 15
playground/ios/NavigationTests/RNNNavigationControllerTest.m Целия файл

@@ -123,7 +123,7 @@
123 123
 	[_vc1 overrideOptions:_options];
124 124
 	
125 125
 	[self.uut popViewControllerAnimated:NO];
126
-	XCTAssertEqual(_vc1.resolveOptions.topBar.background.color.get, self.uut.navigationBar.barTintColor);
126
+	XCTAssertEqual(_vc1.resolveOptions.topBar.background.color.get, self.uut.navigationBar.standardAppearance.backgroundColor);
127 127
 }
128 128
 
129 129
 - (void)testPopViewControllerSetDefaultTopBarBackgroundForPoppingViewController {
@@ -155,7 +155,6 @@
155 155
 - (void)testSetTopBarBackgroundColor_ShouldSetBackgroundColor {
156 156
 	UIColor* color = UIColor.redColor;
157 157
 	[self.uut setTopBarBackgroundColor:color];
158
-	XCTAssertEqual(self.uut.navigationBar.barTintColor, color);
159 158
 	XCTAssertEqual(self.uut.navigationBar.standardAppearance.backgroundColor, color);
160 159
 	XCTAssertEqual(self.uut.navigationBar.compactAppearance.backgroundColor, color);
161 160
 	XCTAssertEqual(self.uut.navigationBar.scrollEdgeAppearance.backgroundColor, color);
@@ -165,25 +164,12 @@
165 164
 	UIColor* transparentColor = UIColor.clearColor;
166 165
 	[self.uut setTopBarBackgroundColor:transparentColor];
167 166
 
168
-	XCTAssertEqual(self.uut.navigationBar.backgroundColor, transparentColor);
169 167
 	XCTAssertTrue(self.uut.navigationBar.translucent);
170
-	XCTAssertNotNil(self.uut.navigationBar.shadowImage);
171
-	XCTAssertNotNil([self.uut.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault]);
172 168
 	XCTAssertNil(self.uut.navigationBar.standardAppearance.backgroundColor);
173 169
 	XCTAssertNil(self.uut.navigationBar.compactAppearance.backgroundColor);
174 170
 	XCTAssertNil(self.uut.navigationBar.scrollEdgeAppearance.backgroundColor);
175 171
 }
176 172
 
177
-- (void)testSetTopBarBackgroundColor_ShouldRemoveTransparentView {
178
-	UIColor* transparentColor = UIColor.clearColor;
179
-	UIColor* redColor = UIColor.redColor;
180
-	
181
-	[self.uut setTopBarBackgroundColor:transparentColor];
182
-	XCTAssertNotNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
183
-	[self.uut setTopBarBackgroundColor:redColor];
184
-	XCTAssertNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
185
-}
186
-
187 173
 - (void)testSetTopBarBackgroundColor_NilColorShouldResetNavigationBar {
188 174
 	UIColor* transparentColor = UIColor.clearColor;
189 175
 	UIColor* redColor = UIColor.redColor;

+ 1
- 11
playground/ios/NavigationTests/RNNRootViewControllerTest.m Целия файл

@@ -60,7 +60,7 @@
60 60
 	[self.uut viewWillAppear:false];
61 61
 	UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
62 62
 
63
-	XCTAssertTrue([self.uut.navigationController.navigationBar.barTintColor isEqual:expectedColor]);
63
+	XCTAssertTrue([self.uut.navigationController.navigationBar.standardAppearance.backgroundColor isEqual:expectedColor]);
64 64
 }
65 65
 
66 66
 -(void)testTopBarBackgroundColorWithoutNavigationController{
@@ -177,16 +177,6 @@
177 177
 
178 178
 }
179 179
 
180
--(void)testTopBarTransparent_BOOL_True {
181
-	UIColor* transparentColor = [RCTConvert UIColor:@(0x00000000)];
182
-	self.options.topBar.background.color = [[Color alloc] initWithValue:transparentColor];
183
-	__unused RNNStackController* nav = [self createNavigationController];
184
-	[self.uut viewWillAppear:false];
185
-	UIView* transparentView = [self.uut.navigationController.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
186
-	XCTAssertTrue(transparentView);
187
-	XCTAssertTrue([NSStringFromCGRect(transparentView.frame) isEqual: NSStringFromCGRect(CGRectZero)]);
188
-}
189
-
190 180
 -(void)testTopBarTransparent_BOOL_false {
191 181
 	UIColor* inputColor = [RCTConvert UIColor:@(0xFFFF0000)];
192 182
 	__unused RNNStackController* nav = [self createNavigationController];