美文网首页iOS Developer
点击状态栏 scrollView 不能返回顶部

点击状态栏 scrollView 不能返回顶部

作者: huiyuM | 来源:发表于2017-06-12 11:32 被阅读30次

现在好多 app 都采用了多个 tableViewcollectionView 放到 scrollView 左右滑动来切换不同页面的设计,这时候就会导致系统默认的点击状态栏使当前 window 最上层 scrollView (或其子类)滚动到最顶部的功能屏蔽,想要恢复这个功能大概有两种方法:

  1. 把当前 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.

相关文章

网友评论

    本文标题:点击状态栏 scrollView 不能返回顶部

    本文链接:https://www.haomeiwen.com/subject/hkhlqxtx.html