简体   繁体   English

如果 TableView 中有 HeaderView,Swift 更喜欢LargeTitles 不起作用

[英]Swift prefersLargeTitles not working if there is a HeaderView in TableView

I replaced a TableViewController to à simple ViewController (and a TableView inside of course).我将TableViewController替换为简单的ViewController (当然还有TableView )。

I have a problem pretty strange:我有一个很奇怪的问题:

  • If there is NO View inside the TableView: ✅ it's working如果TableView 中没有 View :✅ 它正在工作

  • 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 :当我到达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.您可以通过在故事板中设置preferLargeTitle而不是通过编码来解决此问题。

Select the NavigationBar(not NavigationItem) of your NavigationController in storyboard.在故事板中选择 NavigationController 的 NavigationBar(不是 NavigationItem)。 In attributes inspector, make sure the 'Prefers Large Titles' is enabled.在属性检查器中,确保启用“首选大标题”。

You need to set你需要设置

navigationController.navigationBar.prefersLargeTitles = true

before pushing your TestViewController .在推送您的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 https://www.hackingwithswift.com/example-code/uikit/how-to-enable-large-titles-in-your-navigation-bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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