如何在 iOS 11 上模仿 `navigationBar` 中显示大标题的 `UITableViewController`

How to mimic `UITableViewController` showing of the large titles in `navigationBar` on iOS 11

我正在尝试在我的应用程序中使用 iOS 11 中的 prefersLargeTitles。它在 UITableViewController:

的子类中按预期工作
navigationController?.navigationBar.prefersLargeTitles = true

但是,在一种情况下,我需要继承 UIViewController 并自己添加一个 table 视图。在这种情况下,我需要将 table 约束到自己的视图:

tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.otherView.topAnchor).isActive = true

虽然这些限制如我所料呈现 tableView,但现在导航栏始终使用大标题。我想模仿 UITableViewController 的行为,这样当 tableView 滚动到顶部时,会显示大标题,否则标题会折叠成正常标题。

如何解决?

我注意到 prefersLargeTitle 行为的另一个方面,在某些情况下甚至可以提供更简单、更优雅的解决方案。在我的例子中,viewController 不仅包含 tableView(否则我会简单地使用 UITableViewController,我会得到标准的 prefersLargeTitle 行为 out-of-the-box),而且一些其他的看法。现在我注意到,如果您将 tableView 添加为 viewController.view 的第一个子视图,table 将控制大标题功能:

// this will work
fileprivate func setupInitialHierarchy() {
    self.view.addSubview(tableView)
    self.view.addSubview(logoffButton)
}

在我创建视图层次结构之前:

// for some reason now the tableView will not control the large title
fileprivate func setupInitialHierarchy() {
    self.view.addSubview(logoffButton)
    self.view.addSubview(tableView)
}

因此,如果 tableViewviewController 的第一个子视图 view,我们就会得到标准的大标题行为。

备选方案

但是,如果这不可能,我已经能够通过这种方式以编程方式模拟标准行为:

为响应滚动的 tableView 实现委托方法,然后 运行 使用当前 contentOffset 显示或隐藏大标题的代码(UITableView 继承自 UIScrollView,因此 scrollView 参数在这种情况下指的是 tableView):

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y <= 0 {
        self.navigationItem.largeTitleDisplayMode = .always
    } else {
        self.navigationItem.largeTitleDisplayMode = .never
    }
    self.navigationController?.navigationBar.setNeedsLayout()
    self.view.setNeedsLayout()
    UIView.animate(withDuration: 0.25, animations: {
        self.navigationController?.navigationBar.layoutIfNeeded()
        self.view.layoutIfNeeded()
    })
}

请记住,scrollViewDidScroll 会被重复调用,因此 guard 可能是可取的。