UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.