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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.