From 83f69d4effecfbaaf17af3cebdf8a03b38bfa589 Mon Sep 17 00:00:00 2001 From: sganti564 Date: Tue, 15 Jan 2019 13:43:53 +0000 Subject: [PATCH] Adding hideNavBarOnFocusSearchBar option (#4578) * hidesNavigationBarDuringPresentation SearchBar * Adding hideNavBarOnFocusSearchBar option * Fixing compilation error * Code cleanup * code refactoring --- lib/ios/RNNTopBarOptions.h | 1 + lib/ios/RNNTopBarOptions.m | 3 +-- lib/ios/RNNViewControllerPresenter.m | 12 ++++++++++-- lib/ios/UIViewController+RNNOptions.h | 2 +- lib/ios/UIViewController+RNNOptions.m | 4 +++- lib/src/interfaces/Options.ts | 5 +++++ playground/src/screens/SearchScreen.js | 4 +++- 7 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/ios/RNNTopBarOptions.h b/lib/ios/RNNTopBarOptions.h index e32beea7cb4..ecd4978f6f6 100644 --- a/lib/ios/RNNTopBarOptions.h +++ b/lib/ios/RNNTopBarOptions.h @@ -23,6 +23,7 @@ @property (nonatomic, strong) Bool* animate; @property (nonatomic, strong) Bool* searchBar; @property (nonatomic, strong) Bool* searchBarHiddenWhenScrolling; +@property (nonatomic, strong) Bool* hideNavBarOnFocusSearchBar; @property (nonatomic, strong) Text* testID; @property (nonatomic, strong) Text* barStyle; @property (nonatomic, strong) Text* searchBarPlaceholder; diff --git a/lib/ios/RNNTopBarOptions.m b/lib/ios/RNNTopBarOptions.m index b854013e469..b5bb7829013 100644 --- a/lib/ios/RNNTopBarOptions.m +++ b/lib/ios/RNNTopBarOptions.m @@ -14,7 +14,6 @@ @implementation RNNTopBarOptions - (instancetype)initWithDict:(NSDictionary *)dict { self = [super init]; - self.visible = [BoolParser parse:dict key:@"visible"]; self.hideOnScroll = [BoolParser parse:dict key:@"hideOnScroll"]; self.leftButtonColor = [ColorParser parse:dict key:@"leftButtonColor"]; @@ -26,6 +25,7 @@ - (instancetype)initWithDict:(NSDictionary *)dict { self.animate = [BoolParser parse:dict key:@"animate"]; self.searchBar = [BoolParser parse:dict key:@"searchBar"]; self.searchBarHiddenWhenScrolling = [BoolParser parse:dict key:@"searchBarHiddenWhenScrolling"]; + self.hideNavBarOnFocusSearchBar = [BoolParser parse:dict key:@"hideNavBarOnFocusSearchBar"]; self.testID = [TextParser parse:dict key:@"testID"]; self.barStyle = [TextParser parse:dict key:@"barStyle"]; self.searchBarPlaceholder = [TextParser parse:dict key:@"searchBarPlaceholder"]; @@ -57,7 +57,6 @@ - (instancetype)initWithDict:(NSDictionary *)dict { self.leftButtons = dict[@"leftButtons"]; self.rightButtons = dict[@"rightButtons"]; - return self; } diff --git a/lib/ios/RNNViewControllerPresenter.m b/lib/ios/RNNViewControllerPresenter.m index 0aebdf11e82..0d7bbc0d53f 100644 --- a/lib/ios/RNNViewControllerPresenter.m +++ b/lib/ios/RNNViewControllerPresenter.m @@ -29,7 +29,11 @@ - (void)applyOptions:(RNNNavigationOptions *)options { } if (options.topBar.searchBar.hasValue) { - [viewController rnn_setSearchBarWithPlaceholder:[options.topBar.searchBarPlaceholder getWithDefaultValue:@""]]; + BOOL hideNavBarOnFocusSearchBar = YES; + if (options.topBar.hideNavBarOnFocusSearchBar.hasValue) { + hideNavBarOnFocusSearchBar = options.topBar.hideNavBarOnFocusSearchBar.get; + } + [viewController rnn_setSearchBarWithPlaceholder:[options.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar: hideNavBarOnFocusSearchBar]; } } @@ -65,7 +69,11 @@ - (void)mergeOptions:(RNNNavigationOptions *)newOptions currentOptions:(RNNNavig } if (newOptions.topBar.searchBar.hasValue) { - [viewController rnn_setSearchBarWithPlaceholder:[newOptions.topBar.searchBarPlaceholder getWithDefaultValue:@""]]; + BOOL hideNavBarOnFocusSearchBar = YES; + if (newOptions.topBar.hideNavBarOnFocusSearchBar.hasValue) { + hideNavBarOnFocusSearchBar = newOptions.topBar.hideNavBarOnFocusSearchBar.get; + } + [viewController rnn_setSearchBarWithPlaceholder:[newOptions.topBar.searchBarPlaceholder getWithDefaultValue:@""] hideNavBarOnFocusSearchBar:hideNavBarOnFocusSearchBar]; } if (newOptions.topBar.drawBehind.hasValue) { diff --git a/lib/ios/UIViewController+RNNOptions.h b/lib/ios/UIViewController+RNNOptions.h index 14f21182669..c0b8ac51174 100644 --- a/lib/ios/UIViewController+RNNOptions.h +++ b/lib/ios/UIViewController+RNNOptions.h @@ -8,7 +8,7 @@ - (void)rnn_setModalTransitionStyle:(UIModalTransitionStyle)modalTransitionStyle; -- (void)rnn_setSearchBarWithPlaceholder:(NSString *)placeholder; +- (void)rnn_setSearchBarWithPlaceholder:(NSString *)placeholder hideNavBarOnFocusSearchBar:(BOOL)hideNavBarOnFocusSearchBar; - (void)rnn_setSearchBarHiddenWhenScrolling:(BOOL)searchBarHidden; diff --git a/lib/ios/UIViewController+RNNOptions.m b/lib/ios/UIViewController+RNNOptions.m index 55ab9d3279c..427acd9a26c 100644 --- a/lib/ios/UIViewController+RNNOptions.m +++ b/lib/ios/UIViewController+RNNOptions.m @@ -28,7 +28,8 @@ - (void)rnn_setModalTransitionStyle:(UIModalTransitionStyle)modalTransitionStyle self.modalTransitionStyle = modalTransitionStyle; } -- (void)rnn_setSearchBarWithPlaceholder:(NSString *)placeholder { +- (void)rnn_setSearchBarWithPlaceholder:(NSString *)placeholder + hideNavBarOnFocusSearchBar:(BOOL)hideNavBarOnFocusSearchBar { if (@available(iOS 11.0, *)) { if (!self.navigationItem.searchController) { UISearchController *search = [[UISearchController alloc]initWithSearchResultsController:nil]; @@ -40,6 +41,7 @@ - (void)rnn_setSearchBarWithPlaceholder:(NSString *)placeholder { if (placeholder) { search.searchBar.placeholder = placeholder; } + search.hidesNavigationBarDuringPresentation = hideNavBarOnFocusSearchBar; self.navigationItem.searchController = search; // Fixes #3450, otherwise, UIKit will infer the presentation context to be the root most view controller diff --git a/lib/src/interfaces/Options.ts b/lib/src/interfaces/Options.ts index 5ca02896a42..1e46becef14 100644 --- a/lib/src/interfaces/Options.ts +++ b/lib/src/interfaces/Options.ts @@ -369,6 +369,11 @@ export interface OptionsTopBar { * #### (iOS 11+ specific) */ searchBarPlaceholder?: string; + /** + * Controls Hiding NavBar on focus UISearchBar + * #### (iOS 11+ specific) + */ + hideNavBarOnFocusSearchBar?: boolean; /** * Control the Large Title configuration * #### (iOS 11+ specific) diff --git a/playground/src/screens/SearchScreen.js b/playground/src/screens/SearchScreen.js index aea43bbdae8..c4b49709522 100644 --- a/playground/src/screens/SearchScreen.js +++ b/playground/src/screens/SearchScreen.js @@ -21,6 +21,7 @@ for(let i = 0; i < 200; i++) { class SearchControllerScreen extends Component { static options() { + return { topBar: { title: { @@ -33,7 +34,8 @@ class SearchControllerScreen extends Component { background: { translucent: true }, - searchBarPlaceholder: 'Start Typing' + searchBarPlaceholder: 'Start Typing', + hideNavBarOnFocusSearchBar: false } }; }