UISearchController does not redraw the navigation bar when rotated

I have implemented UISearchController and it works great except ...

When I click on the search bar, the navigation bar disappears beautifully as expected. When I turn the phone into landscape view, I get this view, which makes sense.

enter image description here

However, when I turn the phone back to portrait view (still selected in the search type area), I get the following view.

enter image description here

You can see that the navigation bar never appears. I feel like I'm implementing a basic search controller. What could be the reason for this?

self.venueSearchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.venueSearchController.searchResultsUpdater = self; self.venueSearchController.searchBar.delegate = self; self.venueSearchController.dimsBackgroundDuringPresentation = NO; self.venueSearchController.hidesNavigationBarDuringPresentation = YES; self.venueSearchController.searchBar.frame = CGRectMake(self.venueSearchController.searchBar.frame.origin.x, self.venueSearchController.searchBar.frame.origin.y, self.venueSearchController.searchBar.frame.size.width, 44.0); self.definesPresentationContext = YES; self.navigationController.navigationBar.translucent = YES; self.venueSearchController.searchBar.translucent = YES; self.tableView.tableHeaderView = self.venueSearchController.searchBar; 
+12
ios uisearchbar uisearchcontroller
Feb 17 '15 at 0:57
source share
5 answers

It appears that the UISearchController forgets to reset the searchBar frame when the status bar appears. I think this is probably a bug in the UISearchController ; it seems some of them are listed in radar . It seems that the searchBar superview (which is internal to the UISearchController) ends up with the wrong height. This is frustrating, because the solution therefore includes getting into the hierarchy of searchController views that Apple can change ... you can add iOS version checking so that it runs only for specific versions.

If you add the code below to your view controller, it will be called when the collection of features changes. It verifies that: a) the active search controller, b) the status of the Bar is not hidden, and c) the source of searchBar-y is 0, and if so, it increases the surveillance height by the height of the statusBar that the searchBar moves.

 override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) { let app = UIApplication.sharedApplication() if searchController!.active && !app.statusBarHidden && searchController?.searchBar.frame.origin.y == 0 { if let container = self.searchController?.searchBar.superview { container.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y, container.frame.size.width, container.frame.size.height + app.statusBarFrame.height) } } } 

Goal c

 - (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection { [super traitCollectionDidChange: previousTraitCollection]; if(self.venueSearchController.active && ![UIApplication sharedApplication].statusBarHidden && self.venueSearchController.searchBar.frame.origin.y == 0) { UIView *container = self.venueSearchController.searchBar.superview; container.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y, container.frame.size.width, container.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height); } } 
+15
Feb 25 '15 at 15:04
source share

You may need to implement UIBarPositioningDelegate in your view controller.

 self.venueSearchController.searchBar.delegate = self; 

SearchBar is looking for an answer from this delegate.

 @protocol UISearchBarDelegate <UIBarPositioningDelegate> 

Add the following to your self (I assume this is a ViewController)

 #pragma mark - <UIBarPositioningDelegate> // Make sure NavigationBar is properly top-aligned to Status bar - (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { if (bar == self.venueSearchController.searchBar) { return UIBarPositionTopAttached; } else { // Handle other cases return UIBarPositionAny; } } 
+10
Feb 24 '15 at 14:29
source share

I believe this may be a problem with the way the UITableViewController layout deals with the navigation bar, and not the navigation bar when it appears when you rotate backward.

I think you can solve this problem by replacing your UITableViewController with a UIViewController in which you remove the UITableView. Then set the upper limit on the table view as the top layout guide. Set the side constraints left, right, bottom 0.

This should ensure that the table will always be below the status bar and will still move correctly when the navigation bar returns and returns.

See this discussion: iOS 7: UITableView appears in the status bar

+1
Feb 23 '15 at 22:32
source share

I ran into a similar problem with UISearchController using it with navigationItem . This looks different, but caused by the same problem: UISearchController does NOT update the searchBar frame when the status bar appears.

But instead of manually adjusting the frames of the searchBar view searchBar you just have to force it to refresh the layout by switching hidesNavigationBarDuringPresentation back and forth while turning to traitCollectionDidChange or viewWillTransition(to size: CGSize, with cooridnator: UIViewControllerTransitionCoordinator) :

Thus, the fix will look like this:

  override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { super.traitCollectionDidChange(previousTraitCollection) if #available(iOS 11, *) { navigationItem.hidesSearchBarWhenScrolling.toggle() navigationItem.hidesSearchBarWhenScrolling.toggle() } } 

And here is the original issue.

Search in landscape without status bar

Then it spins and got this:

Search in portrait after status bar reappearing

0
Aug 20 '19 at 9:40
source share

I temporarily "fixed" this by disabling "Hide status bar" in the settings.

enter image description here

-6
Feb 24 '15 at 14:21
source share



All Articles