滚动视图在取消侧滑返回时与导航栏"断开连接"

3
第一个视图控制器隐藏了导航栏,第二个视图控制器使用带有大标题的可见导航栏。向前转换没有问题。滚动视图的行为符合预期 - 在向下拉动时导航栏会伸展,并在向上拉动时收缩。但是,当我使用向后轻扫手势并取消它时,滚动视图会“断开”与导航栏的连接,现在它不会再收缩或伸展。 enter image description here 引用: Xcode 11.3,iOS 13.3
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
1个回答

2

这是一个明显的错误。以下是解决方法。在您的表视图控制器中添加此代码:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.navigationController?.view.setNeedsLayout()
}

然而,我必须补充一点,iOS 可能永远不会完全接受根视图控制器隐藏导航栏,而第二个视图控制器显示它的想法。这是一种非常不寻常的配置,可能得不到支持。 - matt
这个真的救了我,我的主要伙计。 - ErikBrandsma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接