第一个视图控制器隐藏了导航栏,第二个视图控制器使用带有大标题的可见导航栏。向前转换没有问题。滚动视图的行为符合预期 - 在向下拉动时导航栏会伸展,并在向上拉动时收缩。但是,当我使用向后轻扫手势并取消它时,滚动视图会“断开”与导航栏的连接,现在它不会再收缩或伸展。
引用:
Xcode 11.3,iOS 13.3
![enter image description here](https://istack.dev59.com/lwOTX.gif)
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .orange
button.addTarget(self, action: #selector(tap), for: .touchUpInside)
setupNavigationController()
}
func setupNavigationController() {
if #available(iOS 13.0, *) {
let coloredAppearance = UINavigationBarAppearance()
coloredAppearance.configureWithOpaqueBackground()
coloredAppearance.backgroundColor = .white
coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.black]
coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black, .font: UIFont.systemFont(ofSize: 26)]
let coloredAppearance2 = UINavigationBarAppearance(barAppearance: coloredAppearance)
coloredAppearance2.shadowColor = nil
navigationController?.navigationBar.standardAppearance = coloredAppearance
navigationController?.navigationBar.scrollEdgeAppearance = coloredAppearance2
}
navigationController?.navigationBar.prefersLargeTitles = true
}
@objc func tap() {
navigationController?.pushViewController(DetailsViewController(), animated: true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: true)
}
}
第二个视图控制器
class DetailsViewController: UIViewController, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
title = "Details"
view.backgroundColor = .systemPink
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.dataSource = self
view.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = {
let identifier = "cell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: identifier) else {
return UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: identifier)
}
return cell
}()
cell.textLabel?.text = "Row \(indexPath.row)"
return cell
}
}
viewWillAppear()
方法中添加tableView.reloadData()
,就可以了! - Nayan Dave