Joe
Joe

Reputation: 131

NavigationBar height not updating after pushViewController (iOS 11)

In the navigationBar I add a searchBar. It changes the height of the navigationBar. I also adjust the frame height to make it look better. All of the following code works fine:

let locationSearchTableVC = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTableVC") as! LocationSearchTableVC
resultSearchController = UISearchController(searchResultsController: locationSearchTableVC)
resultSearchController?.searchResultsUpdater = locationSearchTableVC as UISearchResultsUpdating
self.parent?.navigationItem.titleView = self.resultSearchController?.searchBar

let searchBar = resultSearchController!.searchBar
searchBar.delegate = self
searchBar.sizeToFit()
searchBar.placeholder = searchBarPlaceHolder

resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true

locationSearchTableVC.mapView = mapView
locationSearchTableVC.handleMapSearchDelegate = self

textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideUISearchBar?.textColor = UIColor.gray
let textFieldInsideSearchBarLabel =     self.textFieldInsideUISearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = UIColor.gray
textFieldInsideUISearchBar?.backgroundColor = UIColor(red: 212/255, green: 215/255, blue: 220/255, alpha: 1)

let bounds = self.navigationController!.navigationBar.bounds
let statusBarHeight = UIApplication.shared.statusBarFrame.height

self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + statusBarHeight)

-

When a user taps a tableView cell I transition to another viewController with pushViewController:

self.navigationController?.pushViewController(viewController2, animated: true)

The goal is to maintain the size of the navigationBar after the push but I can't get it to work. Notice in the 2nd picture that it's not quite as tall as the 1st.

enter image description here

enter image description here

I've tried adding a new searchBar (which I'd make translucent), adjusting the frame size, etc. and can't get it to budge.

In fact, if I set the frame size in the 2nd viewController the navigationBar height collapses to take up the area of the statusBar. Very odd.

self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: previousNavBarHeight!)

enter image description here

If I set self.additionalSafeAreaInsets.top I can get the content below the navigationBar pushed down where it should be but the navigationBar itself will not increase in height.

I've seen in other posts that iOS11 changed regarding setting the height but none of the solutions I've tried have worked.

Upvotes: 0

Views: 360

Answers (1)

Joe
Joe

Reputation: 131

So I finally got this to work. I had to try several different ways to change the navigation bar. Here's an example of things to try:

    self.navigationController?.navigationItem.titleView = UISearchBar()
    self.parent?.navigationController?.navigationItem.titleView = UISearchBar()
    self.navigationItem.titleView = UISearchBar()

And this is what worked. I had to add a search bar rather than adjust the height of the navigation bar manually to get it the right size (and then make it transparent, etc.).

    let searchBar = UISearchBar()
    self.navigationItem.titleView = searchBar
    searchBar.setImage(UIImage(), for: UISearchBarIcon.clear, state: .normal)
    searchBar.setImage(UIImage(), for: UISearchBarIcon.search, state: .normal)
    searchBar.isUserInteractionEnabled = false
    let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
    textFieldInsideUISearchBar?.backgroundColor = UIColor.clear

Now when I transition from the 1st view controller (which has a search bar I actually use) to the 2nd view controller the size of the navigation bar stays the same and doesn't bounce around.

Upvotes: -1

Related Questions