小编典典

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

swift

我试图prefersLargeTitles在我的应用程序中使用iOS 11。它按以下子类的预期工作UITableViewController

navigationController?.navigationBar.prefersLargeTitles = true

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

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滚动到顶部时,将显示大标题,否则标题将变为正常标题。

如何解决呢?


阅读 380

收藏
2020-07-07

共1个答案

小编典典

我注意到了prefersLargeTitle行为的另一方面,在某些情况下,它甚至可以提供更简单,更优雅的解决方案。在我的情况下,viewController不仅包含tableView(否则我将简单地使用它,UITableViewController并且将获得标准的prefersLargeTitle行为),还包含一些其他视图。现在,我注意到,如果将添加tableView作为的第一个子视图,则viewController.view该表将控制大标题功能:

// 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)
}

如此看来,如果tableView是第一子视图viewController小号view,我们得到标准的大标题的行为。

替代解决方案

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

实现对的响应的委托方法,tableView以响应滚动,然后运行使用current的代码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可能是可取的。

2020-07-07