How to mimic `UITableViewController` showing of the large titles in `navigationBar` on iOS 11

I’m trying to use prefersLargeTitles from iOS 11 in my application. It works as expected in subclasses of UITableViewController:

navigationController?.navigationBar.prefersLargeTitles = true

However, in one case I needed to subclass UIViewController and add a table view myself. In this situation I needed to constraint the table to the view myself:

  • Swift Documentation Comments
  • Is there a way to have a Swift script use multiple files
  • TVOS remote notification replacement
  • UIImageView changes their position when the UITableViewCell is selected
  • Changing font size in a label for only iPhone 4s, is this possible?
  • how to test asynchronous methods Swift
  • tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
    tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
    tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: self.otherView.topAnchor).isActive = true

    While these constraints present the tableView as I would expect, now the navigation bar always uses large title. I would like to mimic the behavior of the UITableViewController, so that when the tableView is scrolled to top, the large title is presented, otherwise the title collapses into the normal one.

    How to solve this?

    Solutions Collect From Internet About “How to mimic `UITableViewController` showing of the large titles in `navigationBar` on iOS 11”

    I noticed another aspect of prefersLargeTitle behavior that in some cases might provide even a simpler and more elegant solution. In my case, the viewController contained not only the tableView (otherwise I would simply use UITableViewController and I would get standard prefersLargeTitle behavior out-of-the-box), but also some other views. Now I noticed, that if you add the tableView as a first subview of the viewController.view, the table will control the large title feature:

    // this will work
    fileprivate func setupInitialHierarchy() {

    Before I was creating the view hierarchy as follows:

    // for some reason now the tableView will not control the large title
    fileprivate func setupInitialHierarchy() {

    So it seems that if the tableView is the first subview of the viewControllers view, we get the standard large titles behavior.

    Alternative solution

    However, if this is not possible, I have been able to simulate the standard behavior programmatically this way:

    Implementing the delegate method for the tableView that reacts to scrolling, and then running the code that uses current contentOffset to either show, or hide the large title (UITableView inherits from UIScrollView, so the scrollView parameter refers in this case to the tableView):

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y <= 0 {
            self.navigationItem.largeTitleDisplayMode = .always
        } else {
            self.navigationItem.largeTitleDisplayMode = .never
        UIView.animate(withDuration: 0.25, animations: {

    Just remember that scrollViewDidScroll gets called repeatedly, so some guard there might be desirable.