点击statusbar失效及解决

在有scrollView(或其子类)的界面中,当点击状态栏的时候,scrollView会回滚到初始位置,当包含多个scrollView的时候,点击状态栏则不会回滚到初始位置

分析:
scrollView回滚到初始位置与scrollToTop属性有关,默认情况下属性值为true,当有多个scrollView时,系统不知道哪个需要回滚,所以点击无效


解决:

  • 1:设置需要回滚到初始位置scrollView.scrollToTop = true,其它的设置为scrollView.scrollToTop = false
  • 2:当上面的做法失效时,可以使用下面的方法
//可以在AppDelegate中添加这段代码
let statusBarClickNotification = "statusBarClickNotification"
extension AppDelegate{
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
        super.touchesBegan(touches, withEvent: event)
        let location = ((event?.allTouches())! as NSSet).anyObject()?.locationInView(window)
        let statusBarFrame = UIApplication.sharedApplication().statusBarFrame
        if CGRectContainsPoint(statusBarFrame, location!) {
NSNotificationCenter.defaultCenter().postNotificationName(statusBarClickNotification, object: nil)
        }
    }
}
//在需要回滚的scrollView所在的控制器中添加如下代码
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MMMainLiveAnchorVC.statusBarClick), name: statusBarClickNotification, object: nil)
   func statusBarClick() {
        self.collectionView.setContentOffset(CGPointMake(0, 0), animated: true)
//当使用下面的方法时,scrollView可能会把第一页之外的数据给暂时隐藏,所以最好使用上一行代码的方法
//        UIView.animateWithDuration(0.5) {
//            self.collectionView.contentOffset = CGPointMake(0, -0.1)
//        }
    }

你可能感兴趣的:(点击statusbar失效及解决)