1

I've seen an app wherein they can dynamically adjust their status bar height/y-position based on scroll-position.

Here is a sample gif: https://gyazo.com/ec8e3fa336098305e6a5aedc68118789

Here is what I got

override var prefersStatusBarHidden: Bool {
   return isStatusBarHidden
}

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
   if !isStatusBarHidden {
       return UIStatusBarAnimation.fade
   } else {
       return UIStatusBarAnimation.slide
   }
}


func changeStatusBarStatus(status: Bool) {
   isStatusBarHidden = status
   UIView.animate(withDuration: 0.3, delay: 0, options: [.allowUserInteraction], animations: {
       self.setNeedsStatusBarAppearanceUpdate()
   })
}
JohnG
  • 75
  • 10
  • You can hide `StatusBar` and use a `UIView` for that purpose and it will be more convenient and accessible in use – Nayan Dave Dec 19 '19 at 05:02
  • There is a native feature also " navigationController?.hidesBarsOnSwipe = true" . But I am not sure if it will give you the same effect. Create a custom nav bar by using a view , add height constraint to this view. Implement the scrollViewDidScroll method and change the height constraint of the nav bar View based on scrollView contentOffset.y – Help Dec 19 '19 at 05:29
  • @NayanDave, do you mean create a custom status bar? Is it possible? – JohnG Dec 19 '19 at 08:19
  • @Help I think hidesBarsOnSwipe only hides the navbar rather than the status bar. – JohnG Dec 19 '19 at 08:20
  • yes , you're right about @Help's suggestion it will only hide `navigationbar` not `statusbar`.....AND also yes, it's possible to create custom status bar...I'm writing the answer for it , please look at it – Nayan Dave Dec 19 '19 at 09:04

1 Answers1

1

TO CREATE CUSTOM StatusBar

  • Firstly Hide your Default statusBar for the entire app by adding YES/TRUE in Status bar is initially hidden in the info.plist

    This will hide StatusBar from the app unless you add programatically re-show it.

  • Then add a UIView in your ViewController and give it TOP, LEFT & RIGHT constraint (0,0,0) with reference to View(not with reference to Safe Area)

  • Then Give it DOWN or BOTTOM constraint 0 with reference to your Main working UIView

See, I've created a bottom view like that in my app (You just have to reverse this constraint as you are creating StatusBar)

enter image description here

(Note:If you can't see that UIView then use View As iPhone 10 or 11 Series in which you can clearly distinguish it from your other UIView)

Nayan Dave
  • 1,078
  • 1
  • 8
  • 30