我注意到了
prefersLargeTitle行为的另一方面,在某些情况下,它甚至可以提供更简单,更优雅的解决方案。在我的情况下,
viewController不仅包含
tableView(否则我将简单地使用它,
UITableViewController并且将获得标准的
prefersLargeTitle行为),还包含一些其他视图。现在,我注意到,如果将添加
tableView作为的第一个子视图,则
viewController.view该表将控制大标题功能:
// this will workfileprivate func setupInitialHierarchy() { self.view.addSubview(tableView) self.view.addSubview(logoffButton)}在创建视图层次结构之前,如下所示:
// for some reason now the tableView will not control the large titlefileprivate 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可能是可取的。



