[英]UIScrollView + LargeTitle (iOS 11) - scroll to top and reveal the large title
[英]scroll to top of tableview and show large title iOS
我想滚动到 tableView 的顶部,并在点击选项卡栏中的选项卡时在navigationBar
显示largeTitle
。
使用scrollToRow
方法不会显示 largeTitle:
extension TableViewConstroller: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarController.selectedIndex == 1 {
let indexPath = IndexPath(row: 0, section: 0)
tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
}
}
我也试过
self.tableView.scrollRectToVisible(CGRect(x: 0, y: -64, width: 1, height: 1), animated: true)
这有时有效,但如果我向下滚动tableView
,当此方法向上滚动时, navigationBar
tableView
仍会折叠。
我该怎么做才能模仿 tableView 的scrollsToTop
属性的行为,该属性确实滚动到顶部并显示largeTitle
? 它的动画非常流畅并且总是显示大的navigationBar
。
我在UITabBarControllerDelegate
使用了这两种方法:第二种方法搜索第一个scrollView
,然后滚动到该scrollView
的顶部。 由于tableView
也是scrollView
,这种方法有效。
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if tabBarController.selectedViewController == viewController {
if let scrollView = findScrollView(in: viewController.view) {
let desiredOffset = CGPoint(x: 0, y: -scrollView.contentInset.top - 96 + UIApplication.shared.statusBarFrame.height)
scrollView.setContentOffset(desiredOffset, animated: true)
}
}
return true
}
private func findScrollView(in view: UIView) -> UIScrollView? {
if let scrollView = view as? UIScrollView {
return scrollView
} else {
for subview in view.subviews {
if let scrollView = findScrollView(in: subview) {
return scrollView
}
}
}
return nil
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.