【问题标题】:Swift prefersLargeTitles not working if there is a HeaderView in TableView如果 TableView 中有 HeaderView,则 Swift prefersLargeTitles 不起作用
【发布时间】:2019-12-10 09:27:44
【问题描述】:

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

我有一个很奇怪的问题:

  • 如果 TableView 内没有视图:✅ 它工作正常

  • 如果有视图: ❌ 标题默认不是Large,我必须滚动才能以large 模式显示。 (有趣的是 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()
   }

}

【问题讨论】:

  • 嗨凯文。我已经编辑了您的一些问题,因为我注意到您正在使用引用块 (>) 作为一般荧光笔。这不是它的用途 - 它具有特定的语义含义,即它界定了一个引用,即某人或某事所说的东西(在演讲、手册、书籍、杂志、报纸、博客、网络论坛等中)。

标签: ios swift uitableview swift5


【解决方案1】:

您可以通过在情节提要中设置 preferLargeTitle 而不是编码来解决此问题。

在情节提要中选择 NavigationController 的 NavigationBar(不是 NavigationItem)。 在属性检查器中,确保启用了“首选大标题”。

【讨论】:

    【解决方案2】:

    你需要设置

    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

    【讨论】:

    • 该方法没有任何改变。
    • @KevinB,看看我的编辑。你可以在 Xcode Single view playground 中运行它。还是没有帮助?我错过了什么?请更准确。干杯!
    • 我在 tableView 中添加视图(作为标题)时遇到问题
    • 如何将表头添加到tableView? viewForHeaderInSection?
    猜你喜欢
    • 2020-04-24
    • 2020-01-15
    • 1970-01-01
    • 2019-04-07
    • 2016-01-21
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多