简体   繁体   中英

Swift prefersLargeTitles not working if there is a HeaderView in TableView

I replaced a TableViewController to à simple ViewController (and a TableView inside of course).

I have a problem pretty strange:

  • If there is NO View inside the TableView: ✅ it's working

  • If there is a View : ❌ The title is not Large by default, I have to scroll to display it in large mode. (Which is interesting is that the View is not stuck to the top, as you can see with the screenshots)

When I arrive on the ViewController :

在此处输入图片说明

After a scroll:

在此处输入图片说明

This is my code:

class TestViewController: UIViewController {
   @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

      if #available(iOS 11.0, *) {
         self.navigationController?.navigationBar.prefersLargeTitles = true
      }
    }
}

extension TestViewController: UITableViewDataSource, UITableViewDelegate {
   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      0
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      return UITableViewCell()
   }

}

You can fix this by setting preferLargeTitle in storyboard instead of by coding.

Select the NavigationBar(not NavigationItem) of your NavigationController in storyboard. In attributes inspector, make sure the 'Prefers Large Titles' is enabled.

You need to set

navigationController.navigationBar.prefersLargeTitles = true

before pushing your TestViewController .

Works when I run in the playground:

import UIKit
import PlaygroundSupport

final class TestViewController: UIViewController {
    override func loadView() {
        super.loadView()

        let tableView = UITableView(frame: .zero, style: .plain)
        view = tableView
        title = "Large Title"
        view.backgroundColor = .white

        tableView.dataSource = self
        tableView.delegate = self
    }
}

extension TestViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
}

// Present the view controller in the Live View window
let navigationController = UINavigationController(rootViewController: UIViewController())
navigationController.pushViewController(TestViewController(), animated: true)
// before pushing viewController set prefersLargeTitles to true!
navigationController.navigationBar.prefersLargeTitles = true
PlaygroundPage.current.liveView = navigationController

https://www.hackingwithswift.com/example-code/uikit/how-to-enable-large-titles-in-your-navigation-bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM