BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

UITableView scroll position wonky when combined with a UISearchController and UINavigationBar

Forums > iOS

I'm running into this issue involving a UINavigationController with a root view of a UITableViewController that sets a UISearchController in the navigationItem

Broken scroll after popping back to the root

The position of the content in the table view looks fine, until you push a new view controller onto the navigation stack and pop back to the root, where it looks like the contentOffset of the table view gets adjusted incorrectly.

I am able to fix it by setting navigationItem.searchController.isActive = false after pushing the new view controller onto the navigation stack, but I would like to understand what's happening under the hood to cause this

Relevant code:

 private func showReviewViewController(with book: Book) {
        guard let reviewViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ReviewViewController") as? ReviewViewController else { return }

        reviewViewController.viewModel = ReviewViewModel(book: book)
        reviewViewController.delegate = self
        navigationController?.pushViewController(reviewViewController, animated: true)

        /// This fixes things, but why?
        navigationItem.searchController?.isActive = false
}

3      

How are you setting the search controller?

let searchController = UISearchController(searchResultsController: nil)

3      

Here's the search controller code

private func setupSearch() {
        let searchController = UISearchController(searchResultsController: nil)
        searchController.obscuresBackgroundDuringPresentation = false
        definesPresentationContext = true

        navigationItem.hidesSearchBarWhenScrolling = false
        navigationItem.searchController = searchController
        searchBar = searchController.searchBar
        searchBar.delegate = self
        searchBar.placeholder = "Search by author or title"
}

3      

I have the same behaviour in my App, looking for a fix as well ...

3      

Try~ extendedLayoutIncludesOpaqueBars = true

5      

@hdes92404lg's solution worked for me

3      

    edgesForExtendedLayout = .top
    extendedLayoutIncludesOpaqueBars = true

    added above 2 lines in viewDidLoad will resolve the problem. Took very long and found this solution from another website.

3      

@27z  

@maningyt
it works.

3      

3      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.