TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.