Lee Jarvis
Lee Jarvis

Reputation: 16241

iOS 11 TableView to UIView odd space below navigation bar

I have a UIViewController with an embedded UITableView. When a cell in this table view is clicked I use pushViewController to push the destination controller into the navigation controller that all of these are embedded in.

The navigation item has a UISearchBar as a titleView using (in viewDidLoad):

let searchBar = UISearchBar()
searchBar.delegate = self
navigationItem.titleView = searchBar

When the new view controller is pushed and the new view is loaded, there's an odd gap at the bottom of the navigation bar. It seems like it's trying to retain the existing height of the navbar (which is taller due to the search bar), rather than scaling up and down during the transition which is what happens by default. Here's an example (the UIView is painted black to highlight this)

example

As you can see, when I slide back to the table view it seems to fix the second view, but then the offset for the tableview is broken.

I've tried reproducing this with a new Xcode project, but I can't. Everything just works. Obviously I don't actually want to create a new project so I would like to figure out what I've done wrong.

I think this question might be similar:

iOS 7 UITableView: How to remove space between navigation bar and first cell

But I don't want the initial navbar to be smaller in height, and I have also messed with all of the insets that have been suggested in various other questions, but none of those variations are working either.

I have IB constraints on both the tableview and the embedded scrollview in the second controller, they're all set to align to the Safe Area.

I'm guessing there's something obvious I'm missing, but I'm especially confused by the behaviour of swiping back.

Any ideas?

Upvotes: 1

Views: 932

Answers (1)

Lee Jarvis
Lee Jarvis

Reputation: 16241

I ended up "fixing" this by added a viewWillDisappear to my initial view controller:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.view.layoutSubviews()
}

It makes sense to me that this fixes the issue, but it seems quite hacky. I'm investigating other avenues, but since it does constitute a solution to the main problem, I'm going to answer my own question.

Upvotes: 2

Related Questions