UIScrollView动态状态栏

UIScrollView dynamic status bar

我有一个包含 2 个视图的滚动视图:一个 UIImagePicker(Snapchat 风格的相机视图)和一个 UITableView。

滚动视图位于主 viewController 推送到的导航控制器内。我希望状态栏及其上的所有内容(时间、电池、wifi 等)隐藏在相机视图中,但是当您向右滚动到 tableView 时,状态栏内容会重新显示,无论它们是否做了一些滚动时的一种很酷的拉伸动画(弄清楚这一点会很棒)或任何其他可能的解决方案。

希望我的措辞足够让你理解。

你试过打电话给

UIApplication.sharedApplication().setStatusBarHidden(hidden: Bool, withAnimation: UIStatusBarAnimation)

在适当的时间(不推荐这样做,因为您必须将其设置回离开视图时的状态,如您所知)

或覆盖方法

override func prefersStatusBarHidden() -> Bool {
    code
}

在你的控制器中?

我找到的解决方案(更多解决方法) 声明一个名为 hidden 的布尔值。 然后我覆盖了这些方法:

func scrollViewDidScroll(scrollView: UIScrollView){
    let xOffset = scrollView.contentOffset.x;


    if(xOffset > scrollView.contentSize.width/4)
    {
        if hidden == true {
            print("\nShow status bar\n")

            hidden = false
            UIView.animateWithDuration(0.3, animations: {
                self.setNeedsStatusBarAppearanceUpdate()
            })
        }
    } else
    {
       print("\nHide Status Bar\n")

        hidden = true
        UIView.animateWithDuration(0.2, animations: {
            self.setNeedsStatusBarAppearanceUpdate()
        })
    }
}

override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
    if hidden == false {
        return UIStatusBarAnimation.Fade
    } else {
        return UIStatusBarAnimation.Slide
    }
}

override func prefersStatusBarHidden() -> Bool {
    print("\nstatus Bar Changed to hidden = \(hidden)\n")
    return hidden
}

它会在您至少滚动到一半时淡化状态栏,并在您再次返回一半时将状态栏向上滑动。