Search code examples
iosswiftuitableviewios11

How to turn off adjusting large titles by UITableView in iOS 11?


There's this large titles feature in iOS 11 that shows large title when the UITableViewController's table is scrolled to top, and gets collapsed to standard small title when the user scrolls the table away from top. This is standard behavior. I need the navigation controller to behave a bit differently - I need to always show the large title. How to achieve this?

Following code does not help, it still collapses when scrolled.

navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always

Solution

  • I've achieved it unintentionally when embedded UITableViewController inside UIViewController.

    I'm not sure whether it is an Apple's bug or intended behavior.

    So stack is as simple as UINavigationController -> UIViewController(used as container) -> UITableViewController

    Here is sample of view controller with embedded UITableViewController fullscreen

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        var vc = UITableViewController(style: .plain)
        var array: [String] = []
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            vc.view.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(vc.view)
            view.addConstraint(view.leadingAnchor.constraint(equalTo: vc.view.leadingAnchor))
            view.addConstraint(view.rightAnchor.constraint(equalTo: vc.view.rightAnchor))
            view.addConstraint(view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: vc.view.topAnchor))
            view.addConstraint(view.bottomAnchor.constraint(equalTo: vc.view.bottomAnchor))
    
            vc.tableView.delegate = self
            vc.tableView.dataSource = self
    
            array = "0123456789".characters.map(String.init)
            vc.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "identifier")
    
            title = "Title"
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return array.count
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath)
            cell.textLabel?.text = array[indexPath.row]
            return cell
        }
    }
    

    Here is the result

    enter image description here

    Hope it helps.

    P.S. Surprisingly, my current problem is that I don't know how to get collapsing behavior with such architecture :)