繁体   English   中英

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

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

我将TableViewController替换为简单的ViewController (当然还有TableView )。

我有一个很奇怪的问题:

  • 如果TableView 中没有 View :✅ 它正在工作

  • 如果有视图: ❌ 默认情况下标题不是大的,我必须滚动才能以大模式显示它。 (有趣的是,视图并没有粘在顶部,正如您在屏幕截图中看到的那样)

当我到达ViewController

在此处输入图片说明

滚动后:

在此处输入图片说明

这是我的代码:

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()
   }

}

您可以通过在故事板中设置preferLargeTitle而不是通过编码来解决此问题。

在故事板中选择 NavigationController 的 NavigationBar(不是 NavigationItem)。 在属性检查器中,确保启用“首选大标题”。

你需要设置

navigationController.navigationBar.prefersLargeTitles = true

在推送您的TestViewController之前。

当我在操场上跑步时有效:

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

暂无
暂无

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

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