-1

I know change status bar color with under the code.

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .default // or.lightContent
}

But for example, if I use this code with IBAction, how can write the right code?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Scott
  • 137
  • 2
  • 12

2 Answers2

2

When you want your status bar's appearance to change, you can call setNeedsStatusBarAppearanceUpdate on your view controller. In your @IBAction example:

@IBAction func changeStatusBarStyle() -> Void{
    // Do whatever you need to determine and set the next status bar style
    setNeedsStatusBarAppearanceUpdate()
}

Note that if you call setNeedsStatusBarAppearanceUpdate within an animation block, the style change will be animated

Michael Hulet
  • 3,253
  • 1
  • 16
  • 33
0

Set that the status bar needs to be updated when you click on the button. In this way 'preferredStatusBarStyle' will be called.

setNeedsStatusBarAppearanceUpdate()
Pavan Gandhi
  • 1,729
  • 1
  • 22
  • 36
  • preferredStatusBarStyle's example is this? func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.lightContent } – Scott Feb 20 '18 at 04:03