Search code examples
iosswiftuiscrollviewuinavigationcontrolleruistatusbar

UIScrollView dynamic status bar


I have a scroll view with 2 views in it: a UIImagePicker (Snapchat-style camera view), and a UITableView.

The scroll view is inside of a navigation controller that the main viewController pushes to. I want the status bar and everything on it (time, battery, wifi, etc.) to be hidden on the camera view, but when you scroll to the right to the tableView, the status bar contents show back up, whether they do some kind of cool stretch animation as you scroll (would be awesome to figure that out) or any other solution possible.

Hopefully I worded this well enough for you to understand.


Solution

  • Solution I found (More of a workaround) declare a boolean called hidden. Then I overrode these methods:

    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
    }
    

    It fades the status bar in once you've at least scrolled half way, and slides the status bar back up once you've gone back half way again.