Search code examples
iosswiftmodalviewcontrolleruistatusbar

Modal View Controller with Different StatusBar Style than the ParentViewController


I have a view controller with a Light Status Bar Style, and then a user can press a button which modally presents a Game Center Leaderboard. However, I want the modal view controller to have a .Default style when the parent view controller has a .LightContent style. Is this possible to do? Can the modal view controller have a different style than the parent view controller? If so, how would I achieve this?


Solution

  • Well I think I figured out a good solution to my question. Basically when presenting modally, you have a completionHandler. In the completionHandler put these lines of code when you are presenting the modal view:

    self.setStatusBarStyle(.Default)
    self.setNeedsStatusBarAppearanceUpdate()
    

    When you are dismissing the modal view:

    self.setStatusBarStyle(.LightContent)
    self.setNeedsStatusBarAppearanceUpdate()
    

    Make sure not to implement the preferredStatusBarStyle in your View Controller. That method was throwing me off and if you implement the preferredStatusBarStyle method it will override the setStatusBarStyle method and this trick won't work. Basically DO NOT implement preferredStatusBarStyle.