0

My requirements are like, in my app there are two flows, so for both flows value of preferredStatusBarStyle & StatusBar background colour are different. So if I have 40 viewControllers, 20 VCs have same values and 20VCs have different values than first 20 values.

For now I am changing values in each view controllers, like this:-

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

override func viewDidLoad() {
    super.viewDidLoad()

    UIApplication.shared.statusBarView?.backgroundColor = AppColors.themeStatusColor
}

and in info.plist I did this:-

<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

Can u guys tell me any way so I can write these lines of code only two places and I can achieve my requirements.

Pooja Gupta
  • 785
  • 10
  • 22
Vipul Kumar
  • 893
  • 9
  • 19

3 Answers3

1

Create Base view controller(root) and extent it.

class BaseViewController: UIViewController {

   override var preferredStatusBarStyle: UIStatusBarStyle {
      return .lightContent
   }

   override func viewDidLoad() {
      super.viewDidLoad()

    UIApplication.shared.statusBarView?.backgroundColor = AppColors.themeStatusColor
   }
}

Use:

class MYSecondFlowController: BaseViewController {

}
SPatel
  • 4,768
  • 4
  • 32
  • 51
1

You can achieve this by creating two ViewController like below:

// For 20 VC with blue status bar

class BaseVCBlue: UIViewController {

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

 override func viewDidLoad() {
        super.viewDidLoad()

    UIApplication.shared.statusBarView?.backgroundColor = UIColor.blue
    }

// For 20 VC with red status bar

class BaseVCRed: UIViewController {

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

 override func viewDidLoad() {
        super.viewDidLoad()

    UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

when you need blue status bar extend blue and when you want red extend red.

iVarun
  • 6,496
  • 2
  • 26
  • 34
1
class BaseVCBlue: UIViewController {

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.default
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.setNeedsStatusBarAppearanceUpdate()
    UIApplication.shared.statusBarView?.backgroundColor = UIColor.white
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    self.setNeedsStatusBarAppearanceUpdate()
}
}



class BaseVc: UIViewController {

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
}

override func viewDidLoad() {
    super.viewDidLoad()

    UIApplication.shared.statusBarView?.backgroundColor = AppColors.themeStatusColor
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    self.setNeedsStatusBarAppearanceUpdate()
}

But Most important one:- There’s one case where this won’t work and that is if your controller is embedded in a navigation stack . Reason being that iOS wants the parent controller (and NOT the child controller) to decide what kind of status bar one needs to show, then do this also:-

extension UINavigationController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return topViewController?.preferredStatusBarStyle ?? .default
    }
}

Now u can change easily and this will work perfectly :)

Vipul Kumar
  • 893
  • 9
  • 19
Jitendra
  • 5,055
  • 2
  • 22
  • 42