diff --git a/ios/UIViewController+RNNOptions.mm b/ios/UIViewController+RNNOptions.mm index 5c79b2fdac..59ffae5701 100644 --- a/ios/UIViewController+RNNOptions.mm +++ b/ios/UIViewController+RNNOptions.mm @@ -94,6 +94,12 @@ - (void)setSearchBarWithOptions:(NSString *)placeholder } } #endif + if (focus) { + dispatch_async(dispatch_get_main_queue(), ^{ + self.navigationItem.searchController.active = true; + [self.navigationItem.searchController.searchBar becomeFirstResponder]; + }); + } } } diff --git a/playground/ios/NavigationTests/UIViewController+RNNOptionsTest.mm b/playground/ios/NavigationTests/UIViewController+RNNOptionsTest.mm index 2ab918ca79..89211a1739 100644 --- a/playground/ios/NavigationTests/UIViewController+RNNOptionsTest.mm +++ b/playground/ios/NavigationTests/UIViewController+RNNOptionsTest.mm @@ -77,4 +77,25 @@ - (void)testSetBackgroundImageShouldAddUIImageViewSubviewWithImage { XCTAssertEqual(imageView.image, image); } +- (void)testSetSearchBarWithOptionsWithExistingSearchControllerAndFocusShouldFocusSearchBar { + UIViewController *viewController = [UIViewController new]; + UISearchController *searchController = [UISearchController new]; + id searchBar = [OCMockObject partialMockForObject:searchController.searchBar]; + viewController.navigationItem.searchController = searchController; + + [[searchBar expect] becomeFirstResponder]; + + [viewController setSearchBarWithOptions:nil + focus:YES + hideTopBarOnFocus:NO + hideOnScroll:NO + obscuresBackgroundDuringPresentation:NO + backgroundColor:nil + tintColor:nil + cancelText:nil + placement:SearchBarPlacementStacked]; + + [searchBar verifyWithDelay:1]; +} + @end