forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCCViewController.m
executable file
·817 lines (654 loc) · 32.1 KB
/
RCCViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
#import "RCCViewController.h"
#import "RCCNavigationController.h"
#import "RCCTabBarController.h"
#import "RCCDrawerController.h"
#import "RCCTheSideBarManagerViewController.h"
#import <React/RCTRootView.h>
#import "RCCManager.h"
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import "RCCExternalViewControllerProtocol.h"
#import "RCTHelpers.h"
#import "RCCTitleViewHelper.h"
#import "RCCCustomTitleView.h"
NSString* const RCCViewControllerCancelReactTouchesNotification = @"RCCViewControllerCancelReactTouchesNotification";
const NSInteger BLUR_STATUS_TAG = 78264801;
const NSInteger BLUR_NAVBAR_TAG = 78264802;
const NSInteger TRANSPARENT_NAVBAR_TAG = 78264803;
@interface RCCViewController() <UIGestureRecognizerDelegate>
@property (nonatomic) BOOL _hidesBottomBarWhenPushed;
@property (nonatomic) BOOL _statusBarHideWithNavBar;
@property (nonatomic) BOOL _statusBarHidden;
@property (nonatomic) BOOL _statusBarTextColorSchemeLight;
@property (nonatomic, strong) NSDictionary *originalNavBarImages;
@property (nonatomic, strong) UIImageView *navBarHairlineImageView;
@property (nonatomic, weak) id <UIGestureRecognizerDelegate> originalInteractivePopGestureDelegate;
@end
@implementation RCCViewController
-(UIImageView *)navBarHairlineImageView {
if (!_navBarHairlineImageView) {
_navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
}
return _navBarHairlineImageView;
}
+ (UIViewController*)controllerWithLayout:(NSDictionary *)layout globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
{
UIViewController* controller = nil;
if (!layout) return nil;
// get props
if (!layout[@"props"]) return nil;
if (![layout[@"props"] isKindOfClass:[NSDictionary class]]) return nil;
NSDictionary *props = layout[@"props"];
// get children
if (!layout[@"children"]) return nil;
if (![layout[@"children"] isKindOfClass:[NSArray class]]) return nil;
NSArray *children = layout[@"children"];
// create according to type
NSString *type = layout[@"type"];
if (!type) return nil;
// regular view controller
if ([type isEqualToString:@"ViewControllerIOS"])
{
controller = [[RCCViewController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
}
// navigation controller
if ([type isEqualToString:@"NavigationControllerIOS"])
{
controller = [[RCCNavigationController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
}
// tab bar controller
if ([type isEqualToString:@"TabBarControllerIOS"])
{
controller = [[RCCTabBarController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
}
// side menu controller
if ([type isEqualToString:@"DrawerControllerIOS"])
{
NSString *drawerType = props[@"type"];
if ([drawerType isEqualToString:@"TheSideBar"]) {
controller = [[RCCTheSideBarManagerViewController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
}
else {
controller = [[RCCDrawerController alloc] initWithProps:props children:children globalProps:globalProps bridge:bridge];
}
}
// register the controller if we have an id
NSString *componentId = props[@"id"];
if (controller && componentId)
{
[[RCCManager sharedInstance] registerController:controller componentId:componentId componentType:type];
if([controller isKindOfClass:[RCCViewController class]])
{
((RCCViewController*)controller).controllerId = componentId;
}
}
// set background image at root level
NSString *rootBackgroundImageName = props[@"style"][@"rootBackgroundImageName"];
if (rootBackgroundImageName) {
UIImage *image = [UIImage imageNamed: rootBackgroundImageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[controller.view insertSubview:imageView atIndex:0];
}
return controller;
}
-(NSDictionary*)addCommandTypeAndTimestampIfExists:(NSDictionary*)globalProps passProps:(NSDictionary*)passProps {
NSMutableDictionary *modifiedPassProps = [NSMutableDictionary dictionaryWithDictionary:passProps];
NSString *commandType = globalProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE];
if (commandType) {
modifiedPassProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE] = commandType;
}
NSString *timestamp = globalProps[GLOBAL_SCREEN_ACTION_TIMESTAMP];
if (timestamp) {
modifiedPassProps[GLOBAL_SCREEN_ACTION_TIMESTAMP] = timestamp;
}
return modifiedPassProps;
}
- (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
{
NSString *component = props[@"component"];
if (!component) return nil;
NSDictionary *passProps = props[@"passProps"];
NSDictionary *navigatorStyle = props[@"style"];
NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
[mergedProps addEntriesFromDictionary:passProps];
RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
if (!reactView) return nil;
self = [super init];
if (!self) return nil;
[self commonInit:reactView navigatorStyle:navigatorStyle props:props];
self.navigationController.interactivePopGestureRecognizer.delegate = self;
return self;
}
- (instancetype)initWithComponent:(NSString *)component passProps:(NSDictionary *)passProps navigatorStyle:(NSDictionary*)navigatorStyle globalProps:(NSDictionary *)globalProps bridge:(RCTBridge *)bridge
{
NSMutableDictionary *mergedProps = [NSMutableDictionary dictionaryWithDictionary:globalProps];
[mergedProps addEntriesFromDictionary:passProps];
RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:mergedProps];
if (!reactView) return nil;
self = [super init];
if (!self) return nil;
NSDictionary *modifiedPassProps = [self addCommandTypeAndTimestampIfExists:globalProps passProps:passProps];
[self commonInit:reactView navigatorStyle:navigatorStyle props:modifiedPassProps];
return self;
}
- (void)commonInit:(RCTRootView*)reactView navigatorStyle:(NSDictionary*)navigatorStyle props:(NSDictionary*)props
{
self.view = reactView;
self.edgesForExtendedLayout = UIRectEdgeNone; // default
self.automaticallyAdjustsScrollViewInsets = NO; // default
self.navigatorStyle = [NSMutableDictionary dictionaryWithDictionary:[[RCCManager sharedInstance] getAppStyle]];
[self.navigatorStyle addEntriesFromDictionary:navigatorStyle];
[self setStyleOnInit];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTJavaScriptWillStartLoadingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onCancelReactTouches) name:RCCViewControllerCancelReactTouchesNotification object:nil];
self.commandType = props[GLOBAL_SCREEN_ACTION_COMMAND_TYPE];
self.timestamp = props[GLOBAL_SCREEN_ACTION_TIMESTAMP];
// In order to support 3rd party native ViewControllers, we support passing a class name as a prop named `ExternalNativeScreenClass`
// In this case, we create an instance and add it as a child ViewController which preserves the VC lifecycle.
// In case some props are necessary in the native ViewController, the ExternalNativeScreenProps can be used to pass them
[self addExternalVCIfNecessary:props];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.view = nil;
}
-(void)onRNReload
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.view = nil;
}
-(void)onCancelReactTouches
{
if ([self.view isKindOfClass:[RCTRootView class]]){
[(RCTRootView*)self.view cancelTouches];
}
}
- (void)sendScreenChangedEvent:(NSString *)eventName
{
if ([self.view isKindOfClass:[RCTRootView class]]){
RCTRootView *rootView = (RCTRootView *)self.view;
if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
[[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:@
{
@"type": @"ScreenChangedEvent",
@"id": eventName
}];
}
}
}
- (void)sendGlobalScreenEvent:(NSString *)eventName endTimestampString:(NSString *)endTimestampStr shouldReset:(BOOL)shouldReset {
if (!self.commandType) return;
if ([self.view isKindOfClass:[RCTRootView class]]){
NSString *screenName = [((RCTRootView*)self.view) moduleName];
[[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:eventName body:@
{
@"commandType": self.commandType ? self.commandType : @"",
@"screen": screenName ? screenName : @"",
@"startTime": self.timestamp ? self.timestamp : @"",
@"endTime": endTimestampStr ? endTimestampStr : @""
}];
if (shouldReset) {
self.commandType = nil;
self.timestamp = nil;
}
}
}
-(BOOL)isDisappearTriggeredFromPop:(NSString *)eventName {
NSArray *navigationViewControllers = self.navigationController.viewControllers;
if (navigationViewControllers.lastObject == self || [navigationViewControllers indexOfObject:self] == NSNotFound) {
return YES;
}
return NO;
}
- (NSString *)getTimestampString {
long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
return [NSString stringWithFormat:@"%lld", milliseconds];
}
// This is walk around for React-Native bug.
// https://github.com/wix/react-native-navigation/issues/1446
//
// Buttons in ScrollView after changing route/pushing/showing modal
// while there is a momentum scroll are not clickable.
// Back to normal after user start scroll with momentum
- (void)_traverseAndCall:(UIView*)view
{
if([view isKindOfClass:[UIScrollView class]] && ([[(UIScrollView*)view delegate] respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) ) {
dispatch_async(dispatch_get_main_queue(), ^{
[[(UIScrollView*)view delegate] scrollViewDidEndDecelerating:(id)view];
});
}
[view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self _traverseAndCall:obj];
}];
}
// fix iOS11 safeArea - https://github.com/facebook/react-native/issues/15681
// rnn issue - https://github.com/wix/react-native-navigation/issues/1858
- (void)_traverseAndFixScrollViewSafeArea:(UIView *)view {
#ifdef __IPHONE_11_0
if ([view isKindOfClass:UIScrollView.class] && [view respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
[((UIScrollView*)view) setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
[view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self _traverseAndFixScrollViewSafeArea:obj];
}];
#endif
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self sendGlobalScreenEvent:@"didAppear" endTimestampString:[self getTimestampString] shouldReset:YES];
[self sendScreenChangedEvent:@"didAppear"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self _traverseAndFixScrollViewSafeArea:self.view];
[self sendGlobalScreenEvent:@"willAppear" endTimestampString:[self getTimestampString] shouldReset:NO];
[self sendScreenChangedEvent:@"willAppear"];
[self setStyleOnAppear];
}
- (void)viewDidDisappear:(BOOL)animated
{
[self _traverseAndCall:self.view];
[super viewDidDisappear:animated];
[self sendGlobalScreenEvent:@"didDisappear" endTimestampString:[self getTimestampString] shouldReset:YES];
[self sendScreenChangedEvent:@"didDisappear"];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self sendGlobalScreenEvent:@"willDisappear" endTimestampString:[self getTimestampString] shouldReset:NO];
[self sendScreenChangedEvent:@"willDisappear"];
[self setStyleOnDisappear];
}
// most styles should be set here since when we pop a view controller that changed them
// we want to reset the style to what we expect (so we need to reset on every willAppear)
- (void)setStyleOnAppear
{
[self setStyleOnAppearForViewController:self appeared:false];
}
- (void)updateStyle
{
[self setStyleOnAppearForViewController:self appeared:true];
}
-(void)setStyleOnAppearForViewController:(UIViewController*)viewController appeared:(BOOL)appeared
{
NSString *screenBackgroundColor = self.navigatorStyle[@"screenBackgroundColor"];
if (screenBackgroundColor) {
UIColor *color = screenBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:screenBackgroundColor] : nil;
viewController.view.backgroundColor = color;
}
NSString *screenBackgroundImageName = self.navigatorStyle[@"screenBackgroundImageName"];
if (screenBackgroundImageName) {
UIImage *image = [UIImage imageNamed: screenBackgroundImageName];
viewController.view.layer.contents = (__bridge id _Nullable)(image.CGImage);
}
NSString *navBarBackgroundColor = self.navigatorStyle[@"navBarBackgroundColor"];
if (navBarBackgroundColor) {
UIColor *color = navBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarBackgroundColor] : nil;
viewController.navigationController.navigationBar.barTintColor = color;
} else {
viewController.navigationController.navigationBar.barTintColor = nil;
}
NSMutableDictionary *titleTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarText" baseFont:[UIFont boldSystemFontOfSize:17]];
[self.navigationController.navigationBar setTitleTextAttributes:titleTextAttributes];
if (self.navigationItem.titleView && [self.navigationItem.titleView isKindOfClass:[RCCTitleView class]]) {
RCCTitleView *titleView = (RCCTitleView *)self.navigationItem.titleView;
RCCTitleViewHelper *helper = [[RCCTitleViewHelper alloc] init:viewController navigationController:viewController.navigationController title:titleView.titleLabel.text subtitle:titleView.subtitleLabel.text titleImageData:nil isSetSubtitle:NO];
[helper setup:self.navigatorStyle];
}
NSMutableDictionary *navButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarButton"];
NSMutableDictionary *leftNavButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarLeftButton"];
NSMutableDictionary *rightNavButtonTextAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"navBarRightButton"];
if (
navButtonTextAttributes.allKeys.count > 0 ||
leftNavButtonTextAttributes.allKeys.count > 0 ||
rightNavButtonTextAttributes.allKeys.count > 0
) {
for (UIBarButtonItem *item in viewController.navigationItem.leftBarButtonItems) {
[item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
if (leftNavButtonTextAttributes.allKeys.count > 0) {
[item setTitleTextAttributes:leftNavButtonTextAttributes forState:UIControlStateNormal];
}
}
for (UIBarButtonItem *item in viewController.navigationItem.rightBarButtonItems) {
[item setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
if (rightNavButtonTextAttributes.allKeys.count > 0) {
[item setTitleTextAttributes:rightNavButtonTextAttributes forState:UIControlStateNormal];
}
}
// At the moment, this seems to be the only thing that gets the back button correctly
[navButtonTextAttributes removeObjectForKey:NSForegroundColorAttributeName];
[[UIBarButtonItem appearance] setTitleTextAttributes:navButtonTextAttributes forState:UIControlStateNormal];
}
NSString *navBarButtonColor = self.navigatorStyle[@"navBarButtonColor"];
if (navBarButtonColor) {
UIColor *color = navBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:navBarButtonColor] : nil;
viewController.navigationController.navigationBar.tintColor = color;
} else
{
viewController.navigationController.navigationBar.tintColor = nil;
}
BOOL viewControllerBasedStatusBar = false;
NSObject *viewControllerBasedStatusBarAppearance = [[NSBundle mainBundle] infoDictionary][@"UIViewControllerBasedStatusBarAppearance"];
if (viewControllerBasedStatusBarAppearance && [viewControllerBasedStatusBarAppearance isKindOfClass:[NSNumber class]]) {
viewControllerBasedStatusBar = [(NSNumber *)viewControllerBasedStatusBarAppearance boolValue];
}
NSString *statusBarTextColorSchemeSingleScreen = self.navigatorStyle[@"statusBarTextColorSchemeSingleScreen"];
NSString *statusBarTextColorScheme = self.navigatorStyle[@"statusBarTextColorScheme"];
NSString *finalColorScheme = statusBarTextColorSchemeSingleScreen ? : statusBarTextColorScheme;
if (finalColorScheme && [finalColorScheme isEqualToString:@"light"]) {
if (!statusBarTextColorSchemeSingleScreen) {
viewController.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}
self._statusBarTextColorSchemeLight = true;
if (!viewControllerBasedStatusBarAppearance) {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
[viewController setNeedsStatusBarAppearanceUpdate];
} else {
if (!statusBarTextColorSchemeSingleScreen) {
viewController.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}
self._statusBarTextColorSchemeLight = false;
if (!viewControllerBasedStatusBarAppearance) {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
[viewController setNeedsStatusBarAppearanceUpdate];
}
NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
if (tabBarHiddenBool) {
UITabBar *tabBar = viewController.tabBarController.tabBar;
tabBar.transform = CGAffineTransformMakeTranslation(0, tabBar.frame.size.height);
}
NSNumber *navBarHidden = self.navigatorStyle[@"navBarHidden"];
BOOL navBarHiddenBool = navBarHidden ? [navBarHidden boolValue] : NO;
if (viewController.navigationController.navigationBarHidden != navBarHiddenBool) {
[viewController.navigationController setNavigationBarHidden:navBarHiddenBool animated:YES];
}
NSNumber *navBarHideOnScroll = self.navigatorStyle[@"navBarHideOnScroll"];
BOOL navBarHideOnScrollBool = navBarHideOnScroll ? [navBarHideOnScroll boolValue] : NO;
if (navBarHideOnScrollBool) {
viewController.navigationController.hidesBarsOnSwipe = YES;
} else {
viewController.navigationController.hidesBarsOnSwipe = NO;
}
NSNumber *statusBarBlur = self.navigatorStyle[@"statusBarBlur"];
BOOL statusBarBlurBool = statusBarBlur ? [statusBarBlur boolValue] : NO;
if (statusBarBlurBool && ![viewController.view viewWithTag:BLUR_STATUS_TAG]) {
UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
blur.frame = [[UIApplication sharedApplication] statusBarFrame];
blur.tag = BLUR_STATUS_TAG;
[viewController.view insertSubview:blur atIndex:0];
}
NSNumber *navBarBlur = self.navigatorStyle[@"navBarBlur"];
BOOL navBarBlurBool = navBarBlur ? [navBarBlur boolValue] : NO;
if (navBarBlurBool) {
if (![viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG]) {
[self storeOriginalNavBarImages];
[viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
viewController.navigationController.navigationBar.shadowImage = [UIImage new];
UIVisualEffectView *blur = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
blur.frame = CGRectMake(0, -1 * statusBarFrame.size.height, viewController.navigationController.navigationBar.frame.size.width, viewController.navigationController.navigationBar.frame.size.height + statusBarFrame.size.height);
blur.userInteractionEnabled = NO;
blur.tag = BLUR_NAVBAR_TAG;
[viewController.navigationController.navigationBar insertSubview:blur atIndex:0];
[viewController.navigationController.navigationBar sendSubviewToBack:blur];
}
} else {
UIView *blur = [viewController.navigationController.navigationBar viewWithTag:BLUR_NAVBAR_TAG];
if (blur) {
[blur removeFromSuperview];
[viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
self.originalNavBarImages = nil;
}
}
NSNumber *navBarTransparent = self.navigatorStyle[@"navBarTransparent"];
BOOL navBarTransparentBool = navBarTransparent ? [navBarTransparent boolValue] : NO;
void (^action)() = ^ {
if (navBarTransparentBool)
{
if (![viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG])
{
[self storeOriginalNavBarImages];
[viewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
viewController.navigationController.navigationBar.shadowImage = [UIImage new];
UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
transparentView.tag = TRANSPARENT_NAVBAR_TAG;
[viewController.navigationController.navigationBar insertSubview:transparentView atIndex:0];
}
}
else
{
UIView *transparentView = [viewController.navigationController.navigationBar viewWithTag:TRANSPARENT_NAVBAR_TAG];
if (transparentView)
{
[transparentView removeFromSuperview];
[viewController.navigationController.navigationBar setBackgroundImage:self.originalNavBarImages[@"bgImage"] forBarMetrics:UIBarMetricsDefault];
viewController.navigationController.navigationBar.shadowImage = self.originalNavBarImages[@"shadowImage"];
self.originalNavBarImages = nil;
}
}
};
if (!self.transitionCoordinator || self.transitionCoordinator.initiallyInteractive || !navBarTransparentBool || appeared) {
action();
} else {
UIView* backgroundView = [self.navigationController.navigationBar valueForKey:@"backgroundView"];
CGFloat originalAlpha = backgroundView.alpha;
backgroundView.alpha = navBarTransparentBool ? 0.0 : 1.0;
[self.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
action();
backgroundView.alpha = originalAlpha;
}];
}
NSNumber *autoAdjustsScrollViewInsets = self.navigatorStyle[@"autoAdjustScrollViewInsets"];
viewController.automaticallyAdjustsScrollViewInsets = autoAdjustsScrollViewInsets ? [autoAdjustsScrollViewInsets boolValue] : false;
NSNumber *navBarTranslucent = self.navigatorStyle[@"navBarTranslucent"];
BOOL navBarTranslucentBool = navBarTranslucent ? [navBarTranslucent boolValue] : NO;
if (navBarTranslucentBool || navBarBlurBool) {
viewController.navigationController.navigationBar.translucent = YES;
} else {
viewController.navigationController.navigationBar.translucent = NO;
}
NSNumber *extendedLayoutIncludesOpaqueBars = self.navigatorStyle[@"extendedLayoutIncludesOpaqueBars"];
BOOL extendedLayoutIncludesOpaqueBarsBool = extendedLayoutIncludesOpaqueBars ? [extendedLayoutIncludesOpaqueBars boolValue] : NO;
viewController.extendedLayoutIncludesOpaqueBars = extendedLayoutIncludesOpaqueBarsBool;
NSNumber *drawUnderNavBar = self.navigatorStyle[@"drawUnderNavBar"];
BOOL drawUnderNavBarBool = drawUnderNavBar ? [drawUnderNavBar boolValue] : NO;
if (drawUnderNavBarBool) {
viewController.edgesForExtendedLayout |= UIRectEdgeTop;
}
else {
viewController.edgesForExtendedLayout &= ~UIRectEdgeTop;
}
NSNumber *drawUnderTabBar = self.navigatorStyle[@"drawUnderTabBar"];
BOOL drawUnderTabBarBool = drawUnderTabBar ? [drawUnderTabBar boolValue] : NO;
if (drawUnderTabBarBool) {
viewController.edgesForExtendedLayout |= UIRectEdgeBottom;
} else {
viewController.edgesForExtendedLayout &= ~UIRectEdgeBottom;
}
NSNumber *removeNavBarBorder = self.navigatorStyle[@"navBarNoBorder"];
BOOL removeNavBarBorderBool = removeNavBarBorder ? [removeNavBarBorder boolValue] : NO;
if (removeNavBarBorderBool) {
self.navBarHairlineImageView.hidden = YES;
} else {
self.navBarHairlineImageView.hidden = NO;
}
//Bug fix: in case there is a interactivePopGestureRecognizer, it prevents react-native from getting touch events on the left screen area that the gesture handles
//overriding the delegate of the gesture prevents this from happening while keeping the gesture intact (another option was to disable it completely by demand)
if(self.navigationController.viewControllers.count > 1){
if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil)
{
id <UIGestureRecognizerDelegate> interactivePopGestureRecognizer = self.navigationController.interactivePopGestureRecognizer.delegate;
if (interactivePopGestureRecognizer != nil && interactivePopGestureRecognizer != self)
{
self.originalInteractivePopGestureDelegate = interactivePopGestureRecognizer;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
}
NSString *navBarCustomView = self.navigatorStyle[@"navBarCustomView"];
if (navBarCustomView && ![self.navigationItem.titleView isKindOfClass:[RCCCustomTitleView class]]) {
if ([self.view isKindOfClass:[RCTRootView class]]) {
RCTBridge *bridge = ((RCTRootView*)self.view).bridge;
NSDictionary *initialProps = self.navigatorStyle[@"navBarCustomViewInitialProps"];
RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:navBarCustomView initialProperties:initialProps];
RCCCustomTitleView *titleView = [[RCCCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds subView:reactView alignment:self.navigatorStyle[@"navBarComponentAlignment"]];
titleView.backgroundColor = [UIColor clearColor];
reactView.backgroundColor = [UIColor clearColor];
self.navigationItem.titleView = titleView;
self.navigationItem.titleView.backgroundColor = [UIColor clearColor];
self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
self.navigationItem.titleView.clipsToBounds = YES;
}
}
}
-(void)storeOriginalNavBarImages {
NSMutableDictionary *originalNavBarImages = [@{} mutableCopy];
UIImage *bgImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
if (bgImage != nil) {
originalNavBarImages[@"bgImage"] = bgImage;
}
UIImage *shadowImage = self.navigationController.navigationBar.shadowImage;
if (shadowImage != nil) {
originalNavBarImages[@"shadowImage"] = shadowImage;
}
self.originalNavBarImages = originalNavBarImages;
}
-(void)setStyleOnDisappear {
self.navBarHairlineImageView.hidden = NO;
if (self.navigationController != nil && self.navigationController.interactivePopGestureRecognizer != nil && self.originalInteractivePopGestureDelegate != nil)
{
self.navigationController.interactivePopGestureRecognizer.delegate = self.originalInteractivePopGestureDelegate;
self.originalInteractivePopGestureDelegate = nil;
}
}
// only styles that can't be set on willAppear should be set here
- (void)setStyleOnInit
{
NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
if (tabBarHiddenBool) {
self._hidesBottomBarWhenPushed = YES;
} else {
self._hidesBottomBarWhenPushed = NO;
}
NSNumber *statusBarHideWithNavBar = self.navigatorStyle[@"statusBarHideWithNavBar"];
BOOL statusBarHideWithNavBarBool = statusBarHideWithNavBar ? [statusBarHideWithNavBar boolValue] : NO;
if (statusBarHideWithNavBarBool) {
self._statusBarHideWithNavBar = YES;
} else {
self._statusBarHideWithNavBar = NO;
}
NSNumber *statusBarHidden = self.navigatorStyle[@"statusBarHidden"];
BOOL statusBarHiddenBool = statusBarHidden ? [statusBarHidden boolValue] : NO;
if (statusBarHiddenBool) {
self._statusBarHidden = YES;
} else {
self._statusBarHidden = NO;
}
}
- (BOOL)hidesBottomBarWhenPushed
{
if (!self._hidesBottomBarWhenPushed) return NO;
return (self.navigationController.topViewController == self);
}
- (BOOL)prefersStatusBarHidden
{
if (self._statusBarHidden) {
return YES;
}
if (self._statusBarHideWithNavBar) {
return self.navigationController.isNavigationBarHidden;
} else {
return NO;
}
}
- (void)setNavBarVisibilityChange:(BOOL)animated {
[self.navigationController setNavigationBarHidden:[self.navigatorStyle[@"navBarHidden"] boolValue] animated:animated];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
if (self._statusBarTextColorSchemeLight){
return UIStatusBarStyleLightContent;
} else {
return UIStatusBarStyleDefault;
}
}
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
-(void)addExternalVCIfNecessary:(NSDictionary*)props
{
NSString *externalScreenClass = props[@"externalNativeScreenClass"];
if (externalScreenClass != nil)
{
Class class = NSClassFromString(externalScreenClass);
if (class != NULL)
{
id obj = [[class alloc] init];
if (obj != nil && [obj isKindOfClass:[UIViewController class]] && [obj conformsToProtocol:@protocol(RCCExternalViewControllerProtocol)])
{
((id <RCCExternalViewControllerProtocol>)obj).controllerDelegate = self;
[obj setProps:props[@"externalNativeScreenProps"]];
UIViewController *viewController = (UIViewController*)obj;
[self addChildViewController:viewController];
viewController.view.frame = self.view.bounds;
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
else
{
NSLog(@"addExternalVCIfNecessary: could not create instance. Make sure that your class is a UIViewController whihc confirms to RCCExternalViewControllerProtocol");
}
}
else
{
NSLog(@"addExternalVCIfNecessary: could not create class from string. Check that the proper class name wass passed in ExternalNativeScreenClass");
}
}
}
#pragma mark - NewRelic
- (NSString*) customNewRelicInteractionName
{
NSString *interactionName = nil;
if (self.view != nil && [self.view isKindOfClass:[RCTRootView class]])
{
NSString *moduleName = ((RCTRootView*)self.view).moduleName;
if(moduleName != nil)
{
interactionName = [NSString stringWithFormat:@"RCCViewController: %@", moduleName];
}
}
if (interactionName == nil)
{
interactionName = [NSString stringWithFormat:@"RCCViewController with title: %@", self.title];
}
return interactionName;
}
#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
NSNumber *disabledBackGesture = self.navigatorStyle[@"disabledBackGesture"];
BOOL disabledBackGestureBool = disabledBackGesture ? [disabledBackGesture boolValue] : NO;
return !disabledBackGestureBool;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
NSNumber *disabledSimultaneousGesture = self.navigatorStyle[@"disabledSimultaneousGesture"];
BOOL disabledSimultaneousGestureBool = disabledSimultaneousGesture ? [disabledSimultaneousGesture boolValue] : YES; // make default value of disabledSimultaneousGesture is true
return !disabledSimultaneousGestureBool;
}
@end