Przeglądaj źródła

topBarBlur for ios (#1760)

Graham Chance 7 lat temu
rodzic
commit
d15117e644

+ 1
- 1
README.md Wyświetl plik

@@ -84,7 +84,7 @@ Note:  v1 properties with names beginning with 'navBar' are replaced in v2 with
84 84
 | drawUnderTabBar       |  ✅     |    WIP @gran33     |      [Contribute](CONTRIBUTING.md)       | |
85 85
 | drawUnderTopBar       |  ✅     |    WIP @gran33     |      [Contribute](CONTRIBUTING.md)       ||
86 86
 | statusBarBlur         |  ✅     |    ✅     |      [Contribute](CONTRIBUTING.md)       | @gtchance|
87
-| topBarBlur            | ✅      |    [Contribute](CONTRIBUTING.md)     |      [Contribute](CONTRIBUTING.md)       |
87
+| topBarBlur            | ✅      |    ✅     |      [Contribute](CONTRIBUTING.md)       | @gtchance|
88 88
 | tabBarHidden  |   ✅  |   ✅     |    [Contribute](CONTRIBUTING.md)        | @gtchance|
89 89
 | statusBarTextColorScheme |  ✅   |   in development      |      / iOS specific    |
90 90
 | statusBarTextColorSchemeSingleScreen|  ✅   |     in development    |     / iOS specific      |

+ 2
- 0
lib/ios/RNNNavigationOptions.h Wyświetl plik

@@ -2,6 +2,7 @@
2 2
 #import <UIKit/UIKit.h>
3 3
 
4 4
 extern const NSInteger BLUR_STATUS_TAG;
5
+extern const NSInteger BLUR_TOPBAR_TAG;
5 6
 
6 7
 @interface RNNNavigationOptions : NSObject
7 8
 
@@ -23,6 +24,7 @@ extern const NSInteger BLUR_STATUS_TAG;
23 24
 @property (nonatomic, strong) NSNumber* statusBarBlur;
24 25
 @property (nonatomic, strong) NSNumber* statusBarHideWithTopBar;
25 26
 @property (nonatomic, strong) NSNumber* tabBarHidden;
27
+@property (nonatomic, strong) NSNumber* topBarBlur;
26 28
 
27 29
 -(instancetype)init;
28 30
 -(instancetype)initWithDict:(NSDictionary *)navigationOptions;

+ 27
- 1
lib/ios/RNNNavigationOptions.m Wyświetl plik

@@ -2,6 +2,7 @@
2 2
 #import <React/RCTConvert.h>
3 3
 
4 4
 const NSInteger BLUR_STATUS_TAG = 78264801;
5
+const NSInteger BLUR_TOPBAR_TAG = 78264802;
5 6
 
6 7
 @implementation RNNNavigationOptions
7 8
 
@@ -27,6 +28,7 @@ const NSInteger BLUR_STATUS_TAG = 78264801;
27 28
 	self.rightButtons = [navigationOptions objectForKey:@"rightButtons"];
28 29
 	self.topBarNoBorder = [navigationOptions objectForKey:@"topBarNoBorder"];
29 30
 	self.tabBarHidden = [navigationOptions objectForKey:@"tabBarHidden"];
31
+	self.topBarBlur = [navigationOptions objectForKey:@"topBarBlur"];
30 32
 
31 33
 	return self;
32 34
 }
@@ -137,7 +139,31 @@ const NSInteger BLUR_STATUS_TAG = 78264801;
137 139
 			}
138 140
 		}
139 141
 	}
140
-	
142
+
143
+	if (self.topBarBlur && [self.topBarBlur boolValue]) {
144
+
145
+		if (![viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
146
+
147
+			[viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
148
+			viewController.navigationController.navigationBar.shadowImage = [UIImage new];
149
+			UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
150
+			CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
151
+			blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
152
+			blur.userInteractionEnabled = NO;
153
+			blur.tag = BLUR_TOPBAR_TAG;
154
+			[viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
155
+			[viewController.navigationController.navigationBar sendSubviewToBack:blur];
156
+		}
157
+
158
+	} else {
159
+		UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG];
160
+		if (blur) {
161
+			[viewController.navigationController.navigationBar setBackgroundImage: nil forBarMetrics:UIBarMetricsDefault];
162
+			viewController.navigationController.navigationBar.shadowImage = nil;
163
+			[blur removeFromSuperview];
164
+		}
165
+	}
166
+
141 167
 }
142 168
 
143 169
 @end

+ 23
- 0
lib/ios/ReactNativeNavigationTests/RNNRootViewControllerTest.m Wyświetl plik

@@ -342,4 +342,27 @@
342 342
 	XCTAssertFalse([self.uut hidesBottomBarWhenPushed]);
343 343
 }
344 344
 
345
+-(void)testTopBarBlur_default {
346
+	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
347
+	[self.uut viewWillAppear:false];
348
+	XCTAssertNil([self.uut.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]);
349
+}
350
+
351
+
352
+-(void)testTopBarBlur_false {
353
+	NSNumber* topBarBlurInput = @(0);
354
+	self.options.topBarBlur = topBarBlurInput;
355
+	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
356
+	[self.uut viewWillAppear:false];
357
+	XCTAssertNil([self.uut.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]);
358
+}
359
+
360
+-(void)testTopBarBlur_true {
361
+	NSNumber* topBarBlurInput = @(1);
362
+	self.options.topBarBlur = topBarBlurInput;
363
+	__unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.uut];
364
+	[self.uut viewWillAppear:false];
365
+	XCTAssertNotNil([self.uut.navigationController.navigationBar viewWithTag:BLUR_TOPBAR_TAG]);
366
+}
367
+
345 368
 @end