简体   繁体   中英

Status bar Light content doesnot appear in Navigation Controller using Split view controller

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.

try this

 override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM