Browse Source

Fix large title presentation (#5984)

* Fix largeTitle animation and crash

* Create button if not exist

* Prevents largeTitle transition crash

* Fix unit tests

* Set navigationBar.prefersLargeTitles true on stack initialization

* Bump minimum deployment target to iOS 11.0

* Add unit test
Yogev Ben David 4 years ago
parent
commit
54b285531e
No account linked to committer's email address

+ 1
- 0
lib/ios/RNNStackController.m View File

12
     self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:childViewControllers];
12
     self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:childViewControllers];
13
     _stackDelegate = [[StackControllerDelegate alloc] initWithEventEmitter:self.eventEmitter];
13
     _stackDelegate = [[StackControllerDelegate alloc] initWithEventEmitter:self.eventEmitter];
14
     self.delegate = _stackDelegate;
14
     self.delegate = _stackDelegate;
15
+    self.navigationBar.prefersLargeTitles = YES;
15
     return self;
16
     return self;
16
 }
17
 }
17
 
18
 

+ 0
- 6
lib/ios/RNNStackPresenter.m View File

62
     [_topBarPresenter applyOptions:withDefault.topBar];
62
     [_topBarPresenter applyOptions:withDefault.topBar];
63
     
63
     
64
     [stack setNavigationBarBlur:[withDefault.topBar.background.blur getWithDefaultValue:NO]];
64
     [stack setNavigationBarBlur:[withDefault.topBar.background.blur getWithDefaultValue:NO]];
65
-    [stack setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
66
     [stack setNavigationBarClipsToBounds:[withDefault.topBar.background.clipToBounds getWithDefaultValue:NO]];
65
     [stack setNavigationBarClipsToBounds:[withDefault.topBar.background.clipToBounds getWithDefaultValue:NO]];
67
     [stack setBackButtonColor:[withDefault.topBar.backButton.color getWithDefaultValue:nil]];
66
     [stack setBackButtonColor:[withDefault.topBar.backButton.color getWithDefaultValue:nil]];
68
 	
67
 	
79
 - (void)applyOptionsBeforePopping:(RNNNavigationOptions *)options {
78
 - (void)applyOptionsBeforePopping:(RNNNavigationOptions *)options {
80
     RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
79
     RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
81
     RNNStackController* navigationController = self.stackController;
80
     RNNStackController* navigationController = self.stackController;
82
-    [navigationController setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
83
     [_topBarPresenter applyOptionsBeforePopping:options.topBar];
81
     [_topBarPresenter applyOptionsBeforePopping:options.topBar];
84
 }
82
 }
85
 
83
 
119
         [stack setNavigationBarBlur:[options.topBar.background.blur get]];
117
         [stack setNavigationBarBlur:[options.topBar.background.blur get]];
120
     }
118
     }
121
     
119
     
122
-    if (options.topBar.largeTitle.visible.hasValue) {
123
-        [stack setNavigationBarLargeTitleVisible:options.topBar.largeTitle.visible.get];
124
-    }
125
-    
126
     if (options.topBar.backButton.color.hasValue) {
120
     if (options.topBar.backButton.color.hasValue) {
127
         [stack setBackButtonColor:options.topBar.backButton.color.get];
121
         [stack setBackButtonColor:options.topBar.backButton.color.get];
128
     }
122
     }

+ 7
- 5
lib/ios/TopBarPresenter.m View File

120
     NSNumber* fontSize = [backButtonOptions.fontSize getWithDefaultValue:nil];
120
     NSNumber* fontSize = [backButtonOptions.fontSize getWithDefaultValue:nil];
121
     NSString* testID = [backButtonOptions.testID getWithDefaultValue:nil];
121
     NSString* testID = [backButtonOptions.testID getWithDefaultValue:nil];
122
     
122
     
123
-	UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
124
-    backItem.accessibilityIdentifier = testID;
125
-    
126
     NSArray* stackChildren = self.navigationController.viewControllers;
123
     NSArray* stackChildren = self.navigationController.viewControllers;
124
+    UIViewController *lastViewControllerInStack = stackChildren.count > 1 ? stackChildren[stackChildren.count - 2] : self.navigationController.topViewController;
125
+    UIBarButtonItem *backItem = lastViewControllerInStack.navigationItem.backBarButtonItem ?: [UIBarButtonItem new];
126
+    backItem.accessibilityIdentifier = testID;
127
+
127
     icon = color
128
     icon = color
128
     ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
129
     ? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
129
     : icon;
130
     : icon;
130
     [self setBackIndicatorImage:icon withColor:color];
131
     [self setBackIndicatorImage:icon withColor:color];
131
     
132
     
132
-    UIViewController *lastViewControllerInStack = stackChildren.count > 1 ? stackChildren[stackChildren.count - 2] : self.navigationController.topViewController;
133
-
134
     if (showTitle) {
133
     if (showTitle) {
135
         backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
134
         backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
135
+    } else {
136
+        backItem.title = @"";
136
     }
137
     }
138
+    
137
     backItem.tintColor = color;
139
     backItem.tintColor = color;
138
 	
140
 	
139
     if (fontFamily) {
141
     if (fontFamily) {

+ 0
- 2
lib/ios/UINavigationController+RNNOptions.h View File

16
 
16
 
17
 - (void)setNavigationBarClipsToBounds:(BOOL)clipsToBounds;
17
 - (void)setNavigationBarClipsToBounds:(BOOL)clipsToBounds;
18
 
18
 
19
-- (void)setNavigationBarLargeTitleVisible:(BOOL)visible;
20
-
21
 - (void)setBackButtonColor:(UIColor *)color;
19
 - (void)setBackButtonColor:(UIColor *)color;
22
 
20
 
23
 @end
21
 @end

+ 0
- 10
lib/ios/UINavigationController+RNNOptions.m View File

33
 	self.navigationBar.barStyle = barStyle;
33
 	self.navigationBar.barStyle = barStyle;
34
 }
34
 }
35
 
35
 
36
-- (void)setNavigationBarLargeTitleVisible:(BOOL)visible {
37
-	if (@available(iOS 11.0, *)) {
38
-		if (visible){
39
-			self.navigationBar.prefersLargeTitles = YES;
40
-		} else {
41
-			self.navigationBar.prefersLargeTitles = NO;
42
-		}
43
-	}
44
-}
45
-
46
 - (void)setNavigationBarBlur:(BOOL)blur {
36
 - (void)setNavigationBarBlur:(BOOL)blur {
47
 	if (blur && ![self.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
37
 	if (blur && ![self.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
48
 		[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
38
 		[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

+ 4
- 0
playground/ios/NavigationTests/RNNStackControllerTest.m View File

38
 	XCTAssertNotNil(self.uut.presenter);
38
 	XCTAssertNotNil(self.uut.presenter);
39
 }
39
 }
40
 
40
 
41
+- (void)testInitWithLayoutInfo_shouldPreferLargeTitle {
42
+	XCTAssertTrue(self.uut.navigationBar.prefersLargeTitles);
43
+}
44
+
41
 - (void)testInitWithLayoutInfo_shouldSetMultipleViewControllers {
45
 - (void)testInitWithLayoutInfo_shouldSetMultipleViewControllers {
42
 	self.uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:_creator options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNComponentPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1, _vc2]];
46
 	self.uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:_creator options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNComponentPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1, _vc2]];
43
 	XCTAssertTrue(self.uut.viewControllers.count == 2);
47
 	XCTAssertTrue(self.uut.viewControllers.count == 2);

+ 4
- 9
playground/ios/NavigationTests/RNNStackPresenterTest.m View File

44
 	XCTAssertTrue([_boundViewController.navigationBar.standardAppearance.backgroundColor isEqual:[UIColor redColor]]);
44
 	XCTAssertTrue([_boundViewController.navigationBar.standardAppearance.backgroundColor isEqual:[UIColor redColor]]);
45
 }
45
 }
46
 
46
 
47
-- (void)testApplyOptionsBeforePoppingShouldSetLargeTitleForPoppingViewController {
47
+- (void)testApplyOptionsShouldSetLargeTitleVisible {
48
 	_options.topBar.largeTitle.visible = [[Bool alloc] initWithBOOL:YES];
48
 	_options.topBar.largeTitle.visible = [[Bool alloc] initWithBOOL:YES];
49
 	
49
 	
50
-	[self.uut applyOptionsBeforePopping:self.options];
50
+	[self.uut applyOptions:self.options];
51
 	XCTAssertTrue([[_boundViewController navigationBar] prefersLargeTitles]);
51
 	XCTAssertTrue([[_boundViewController navigationBar] prefersLargeTitles]);
52
 }
52
 }
53
 
53
 
54
-- (void)testApplyOptionsBeforePoppingShouldSetDefaultLargeTitleFalseForPoppingViewController {
55
-	_options.topBar.largeTitle.visible = nil;
56
-	[self.uut applyOptionsBeforePopping:self.options];
57
-	XCTAssertFalse([[_boundViewController navigationBar] prefersLargeTitles]);
58
-}
59
-
60
 - (void)testApplyOptions_shouldSetBackButtonOnBoundViewController_withTitle {
54
 - (void)testApplyOptions_shouldSetBackButtonOnBoundViewController_withTitle {
61
 	Text* title = [[Text alloc] initWithValue:@"Title"];
55
 	Text* title = [[Text alloc] initWithValue:@"Title"];
62
 	self.options.topBar.backButton.title = title;
56
 	self.options.topBar.backButton.title = title;
69
 	self.options.topBar.backButton.title = title;
63
 	self.options.topBar.backButton.title = title;
70
 	self.options.topBar.backButton.showTitle = [[Bool alloc] initWithValue:@(0)];
64
 	self.options.topBar.backButton.showTitle = [[Bool alloc] initWithValue:@(0)];
71
 	[self.uut applyOptions:self.options];
65
 	[self.uut applyOptions:self.options];
72
-	XCTAssertNil(self.boundViewController.viewControllers.firstObject.navigationItem.backBarButtonItem.title);
66
+	NSLog(@"%@", self.boundViewController.viewControllers.firstObject.navigationItem.backBarButtonItem.title);
67
+	XCTAssertTrue([self.boundViewController.viewControllers.firstObject.navigationItem.backBarButtonItem.title isEqualToString:@""]);
73
 }
68
 }
74
 
69
 
75
 - (void)testApplyOptions_shouldSetBackButtonOnBoundViewController_withDefaultValues {
70
 - (void)testApplyOptions_shouldSetBackButtonOnBoundViewController_withDefaultValues {

+ 2
- 0
playground/ios/playground.xcodeproj/project.pbxproj View File

815
 				DEAD_CODE_STRIPPING = NO;
815
 				DEAD_CODE_STRIPPING = NO;
816
 				DEVELOPMENT_TEAM = XPHGA2FMQQ;
816
 				DEVELOPMENT_TEAM = XPHGA2FMQQ;
817
 				INFOPLIST_FILE = playground/Info.plist;
817
 				INFOPLIST_FILE = playground/Info.plist;
818
+				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
818
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
819
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
819
 				OTHER_LDFLAGS = (
820
 				OTHER_LDFLAGS = (
820
 					"$(inherited)",
821
 					"$(inherited)",
838
 				CODE_SIGN_STYLE = Automatic;
839
 				CODE_SIGN_STYLE = Automatic;
839
 				DEVELOPMENT_TEAM = XPHGA2FMQQ;
840
 				DEVELOPMENT_TEAM = XPHGA2FMQQ;
840
 				INFOPLIST_FILE = playground/Info.plist;
841
 				INFOPLIST_FILE = playground/Info.plist;
842
+				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
841
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
843
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
842
 				OTHER_LDFLAGS = (
844
 				OTHER_LDFLAGS = (
843
 					"$(inherited)",
845
 					"$(inherited)",