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


How are you setting the search controller?

let searchController = UISearchController(searchResultsController: nil)


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"


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


Try~ extendedLayoutIncludesOpaqueBars = true


@hdes92404lg's solution worked for me


    edgesForExtendedLayout = .top
    extendedLayoutIncludesOpaqueBars = true

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



it works.



Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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.