0

When adding status bar as light content. its appearing fine in login screen. it changes to white in login screen. after successful login I have an split view and navigation controller.

I have added the code but still it shows black.

1) added below line in view contoller.

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

2) info.plist

View controller-based status bar appearance -> NO

3) then came across this line and added this one also.

  controller.navigationController?.navigationBar.barTintColor = UIColor.white

4) then came across article where it was mentioned to add extension if we need to change status bar in navigation but still nothing works

extension UINavigationController
{
    override open var preferredStatusBarStyle: UIStatusBarStyle {
        get {
            return .lightContent
        }
    }
}

I have added and tried with each of them but still it shows black status bar.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
prateek sharma
  • 175
  • 1
  • 16

3 Answers3

0

try this

 override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}
Habib Alejalil
  • 435
  • 1
  • 4
  • 17
  • no , not working. Its showing error `Type 'NSAttributedStringKey' (aka 'NSString') has no member 'foregroundColor` – prateek sharma Sep 30 '17 at 12:06
  • I added this line : `UINavigationBar.appearance().titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 18)!, NSForegroundColorAttributeName: UIColor.white` But still nothing worked :( ] – prateek sharma Sep 30 '17 at 12:06
  • @prateeksharma Which SWIFT version do you use? – Habib Alejalil Sep 30 '17 at 12:41
  • @prateeksharma use this code : override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } – Habib Alejalil Sep 30 '17 at 18:30
  • see above its not working. The thing was that I'm trying to add this in Split view controller having individual Navigation controller. Its working fine for single view controller but not inside Split and navigation – prateek sharma Oct 01 '17 at 14:10
0

There's another approach to do the same.

self.navigationController?.navigationBar.barStyle = .black

The idea behind this is that UIBarStyle figures out content color of UIStatusBar. Upon passing .black it figures out that background is black and UIStatusBarStyleLightContent mode is required. And when you pass .default it figures out that background is light and returns black content or UIStatusBarStyleDefault.

Codetard
  • 2,441
  • 28
  • 34
0

I have the same problem with Navigation Controller. But I resolve it as below.

Step 1: Create a Custom Navigation class

import UIKit


class CustomNavigationController: UINavigationController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return topViewController?.preferredStatusBarStyle ?? .default
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        
        appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        appearance.backgroundColor = .systemIndigo
        
        navigationBar.standardAppearance = appearance
        navigationBar.scrollEdgeAppearance = appearance
        navigationBar.prefersLargeTitles = true
    }
}

Step2: Use this class to subclass your NavigationColtroller.

Step3: Go to your ViewController who is embedded with a navigation controller.

Stpe4: Put this line of code in viewDidLoad()


// MARK:-  Lifecycle Methods
    override func viewDidLoad() {
self.navigationController?.navigationBar.barStyle = .black
}

Step5: You can override a method in ViewController

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
      }

You are good to go. If you have any doubt plz comment

Kumar Lav
  • 234
  • 1
  • 3
  • 9