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