现在好多 app 都采用了多个 tableView
或 collectionView
放到 scrollView
左右滑动来切换不同页面的设计,这时候就会导致系统默认的点击状态栏使当前 window
最上层 scrollView
(或其子类)滚动到最顶部的功能屏蔽,想要恢复这个功能大概有两种方法:
- 把当前
window
最上层的scrollView
(或其子类)的scrollsToTop
属性设为true
,把当前控制器的其他所有scrollView
(或其子类)的scrollsToTop
属性设为false
- 在
AppDelegate
类中通过监听点击事件,如果点击到statusBar
区域,则发出通知(NSNotification
),(其他相关页面需要先监听这个通知),其他页面在收到通知的时候把当前window
最上层的scrollView
(或其子类)的contentOffet.y
设为初始状态
private typealias TouchStatusAction = AppDelegate
extension TouchStatusAction {
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
let touchLocation = event!.allTouches?.first!.location(in: self.window)
let statusBarFrame = UIApplication.shared.statusBarFrame
if statusBarFrame.contains(touchLocation!) {
self.statusBarTouchedAction()
}
}
func statusBarTouchedAction() {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "statusBarTouchedNotification"), object: nil)
}
}
附录:
- 文档中对
scrollsToTop
的解释
// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.
// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.
open var scrollsToTop: Bool // default is YES.
网友评论