Skip to content

Commit

Permalink
Fix large title presentation (#5984)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yogevbd authored Mar 3, 2020
1 parent c33ff12 commit 54b2855
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 32 deletions.
1 change: 1 addition & 0 deletions lib/ios/RNNStackController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo creator:(id<RNNCo
self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:childViewControllers];
_stackDelegate = [[StackControllerDelegate alloc] initWithEventEmitter:self.eventEmitter];
self.delegate = _stackDelegate;
self.navigationBar.prefersLargeTitles = YES;
return self;
}

Expand Down
6 changes: 0 additions & 6 deletions lib/ios/RNNStackPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ - (void)applyOptions:(RNNNavigationOptions *)options {
[_topBarPresenter applyOptions:withDefault.topBar];

[stack setNavigationBarBlur:[withDefault.topBar.background.blur getWithDefaultValue:NO]];
[stack setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
[stack setNavigationBarClipsToBounds:[withDefault.topBar.background.clipToBounds getWithDefaultValue:NO]];
[stack setBackButtonColor:[withDefault.topBar.backButton.color getWithDefaultValue:nil]];

Expand All @@ -79,7 +78,6 @@ - (void)applyOptionsOnViewDidLayoutSubviews:(RNNNavigationOptions *)options {
- (void)applyOptionsBeforePopping:(RNNNavigationOptions *)options {
RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
RNNStackController* navigationController = self.stackController;
[navigationController setNavigationBarLargeTitleVisible:[withDefault.topBar.largeTitle.visible getWithDefaultValue:NO]];
[_topBarPresenter applyOptionsBeforePopping:options.topBar];
}

Expand Down Expand Up @@ -119,10 +117,6 @@ - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigat
[stack setNavigationBarBlur:[options.topBar.background.blur get]];
}

if (options.topBar.largeTitle.visible.hasValue) {
[stack setNavigationBarLargeTitleVisible:options.topBar.largeTitle.visible.get];
}

if (options.topBar.backButton.color.hasValue) {
[stack setBackButtonColor:options.topBar.backButton.color.get];
}
Expand Down
12 changes: 7 additions & 5 deletions lib/ios/TopBarPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,22 @@ - (void)setBackButtonOptions:(RNNBackButtonOptions *)backButtonOptions {
NSNumber* fontSize = [backButtonOptions.fontSize getWithDefaultValue:nil];
NSString* testID = [backButtonOptions.testID getWithDefaultValue:nil];

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
backItem.accessibilityIdentifier = testID;

NSArray* stackChildren = self.navigationController.viewControllers;
UIViewController *lastViewControllerInStack = stackChildren.count > 1 ? stackChildren[stackChildren.count - 2] : self.navigationController.topViewController;
UIBarButtonItem *backItem = lastViewControllerInStack.navigationItem.backBarButtonItem ?: [UIBarButtonItem new];
backItem.accessibilityIdentifier = testID;

icon = color
? [[icon withTintColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
: icon;
[self setBackIndicatorImage:icon withColor:color];

UIViewController *lastViewControllerInStack = stackChildren.count > 1 ? stackChildren[stackChildren.count - 2] : self.navigationController.topViewController;

if (showTitle) {
backItem.title = title ? title : lastViewControllerInStack.navigationItem.title;
} else {
backItem.title = @"";
}

backItem.tintColor = color;

if (fontFamily) {
Expand Down
2 changes: 0 additions & 2 deletions lib/ios/UINavigationController+RNNOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

- (void)setNavigationBarClipsToBounds:(BOOL)clipsToBounds;

- (void)setNavigationBarLargeTitleVisible:(BOOL)visible;

- (void)setBackButtonColor:(UIColor *)color;

@end
10 changes: 0 additions & 10 deletions lib/ios/UINavigationController+RNNOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ - (void)setBarStyle:(UIBarStyle)barStyle {
self.navigationBar.barStyle = barStyle;
}

- (void)setNavigationBarLargeTitleVisible:(BOOL)visible {
if (@available(iOS 11.0, *)) {
if (visible){
self.navigationBar.prefersLargeTitles = YES;
} else {
self.navigationBar.prefersLargeTitles = NO;
}
}
}

- (void)setNavigationBarBlur:(BOOL)blur {
if (blur && ![self.navigationBar viewWithTag:BLUR_TOPBAR_TAG]) {
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
Expand Down
4 changes: 4 additions & 0 deletions playground/ios/NavigationTests/RNNStackControllerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ - (void)testInitWithLayoutInfo_shouldBindPresenter {
XCTAssertNotNil(self.uut.presenter);
}

- (void)testInitWithLayoutInfo_shouldPreferLargeTitle {
XCTAssertTrue(self.uut.navigationBar.prefersLargeTitles);
}

- (void)testInitWithLayoutInfo_shouldSetMultipleViewControllers {
self.uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:_creator options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNComponentPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1, _vc2]];
XCTAssertTrue(self.uut.viewControllers.count == 2);
Expand Down
13 changes: 4 additions & 9 deletions playground/ios/NavigationTests/RNNStackPresenterTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,13 @@ - (void)testApplyOptionsBeforePoppingShouldSetTopBarBackgroundForPoppingViewCont
XCTAssertTrue([_boundViewController.navigationBar.standardAppearance.backgroundColor isEqual:[UIColor redColor]]);
}

- (void)testApplyOptionsBeforePoppingShouldSetLargeTitleForPoppingViewController {
- (void)testApplyOptionsShouldSetLargeTitleVisible {
_options.topBar.largeTitle.visible = [[Bool alloc] initWithBOOL:YES];

[self.uut applyOptionsBeforePopping:self.options];
[self.uut applyOptions:self.options];
XCTAssertTrue([[_boundViewController navigationBar] prefersLargeTitles]);
}

- (void)testApplyOptionsBeforePoppingShouldSetDefaultLargeTitleFalseForPoppingViewController {
_options.topBar.largeTitle.visible = nil;
[self.uut applyOptionsBeforePopping:self.options];
XCTAssertFalse([[_boundViewController navigationBar] prefersLargeTitles]);
}

- (void)testApplyOptions_shouldSetBackButtonOnBoundViewController_withTitle {
Text* title = [[Text alloc] initWithValue:@"Title"];
self.options.topBar.backButton.title = title;
Expand All @@ -69,7 +63,8 @@ - (void)testApplyOptions_shouldSetBackButtonOnBoundViewController_withHideTitle
self.options.topBar.backButton.title = title;
self.options.topBar.backButton.showTitle = [[Bool alloc] initWithValue:@(0)];
[self.uut applyOptions:self.options];
XCTAssertNil(self.boundViewController.viewControllers.firstObject.navigationItem.backBarButtonItem.title);
NSLog(@"%@", self.boundViewController.viewControllers.firstObject.navigationItem.backBarButtonItem.title);
XCTAssertTrue([self.boundViewController.viewControllers.firstObject.navigationItem.backBarButtonItem.title isEqualToString:@""]);
}

- (void)testApplyOptions_shouldSetBackButtonOnBoundViewController_withDefaultValues {
Expand Down
2 changes: 2 additions & 0 deletions playground/ios/playground.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@
DEAD_CODE_STRIPPING = NO;
DEVELOPMENT_TEAM = XPHGA2FMQQ;
INFOPLIST_FILE = playground/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_LDFLAGS = (
"$(inherited)",
Expand All @@ -838,6 +839,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = XPHGA2FMQQ;
INFOPLIST_FILE = playground/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_LDFLAGS = (
"$(inherited)",
Expand Down

0 comments on commit 54b2855

Please sign in to comment.