Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar increases navigation bar height in iOS 11

I have my UISearchBar being part of the navigation bar like:

 let searchBar = UISearchBar()  //some more configuration to the search bar  .....  navigationItem.titleView = searchBar 

After updating to iOS 11 something weird happened to the search bar in my app. On iOS 10 and prior I had my navigation bar looking like:

enter image description here

Now with iOS 11 I have:

enter image description here

As you can see there is difference in the rounding of the two search bars which does not bothers me. The problem is that the search bar increases the height of the navigation bar. So when I go to another controller it looks weird too:

enter image description here

In fact that weird black line's height plus the current navigation bar's height is equal to the height of navigation bar shown in the second picture ...

Any ideas how to get rid of the black line and having consistent navigation bar height across all view controllers ?

like image 605
radioaktiv Avatar asked Sep 20 '17 09:09

radioaktiv


People also ask

What is navigation bar height iOS?

On iPhone tab bars remain 49 points tall in portrait and 32 points tall in landscape. iPhone X added extra height for the Home Bar to toolbars and tab bars and their sizes are unchanged from iOS 11: 83 points tall in portrait and 53 points tall in landscape.

How do I change the height of a search bar in Swift?

Using searchbar. heightAnchor. constraint(equalToConstant: 200). isActive = true worked for me.


2 Answers

I got black line under NavigationBar with SearchBar in iOS 11 in two cases:

  • when i pushed another ViewControllers from ViewController with UISearchBar enter image description here

  • when i dismissed ViewController with UISearchBar with "drag right to dismiss" enter image description here

My solution was: adding this code to my ViewController with UISearchBar:

-(void)viewWillDisappear:(BOOL)animated{     [super viewWillDisappear:animated];     [self.navigationController.view setNeedsLayout]; // force update layout     [self.navigationController.view layoutIfNeeded]; // to fix height of the navigation bar } 

Swift 4 Update

override func viewWillDisappear(_ animated: Bool) {     super.viewWillDisappear(animated)     navigationController?.view.setNeedsLayout() // force update layout     navigationController?.view.layoutIfNeeded() // to fix height of the navigation bar } 
like image 159
Andrew Avatar answered Sep 28 '22 06:09

Andrew


You can add a constraint of height 44 to the search bar for iOS 11.

// Swift

if #available(iOS 11.0, *) {     searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true } 

// Objective-C

if (@available(iOS 11.0, *)) {     [searchBar.heightAnchor constraintEqualToConstant:44].active = YES; } 
like image 38
zgjie Avatar answered Sep 28 '22 06:09

zgjie