UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
I use this one to change status bar to light in all app. But now I need to change it in just one View Controller back to black. How can I do that?
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
I use this one to change status bar to light in all app. But now I need to change it in just one View Controller back to black. How can I do that?
Set View controller-based status bar appearance
in your project.plist to NO
Use viewWillAppear
and will viewWillDisappear
to set and reset the statusBarStyle, while keeping a property with the previous statusBarStyle like this
let initialStatusBarStyle : UIStatusBarStyle
func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
initialStatusBarStyle = UIApplication.sharedApplication().statusBarStyle
UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: animated)
}
func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().setStatusBarStyle(initialStatusBarStyle, animated: animated)
}
This solution is a little bit different:
import UIKit
@IBDesignable
class DesignableViewController: UIViewController {
@IBInspectable var LightStatusBar: Bool = false
override var preferredStatusBarStyle: UIStatusBarStyle {
get {
if LightStatusBar {
return UIStatusBarStyle.lightContent
} else {
return UIStatusBarStyle.default
}
}
}
}
Change the code for your ViewController(s) from:
class ViewController: UIViewController {
To:
class ViewController: DesignableViewController {
Select the ViewControllers on the Storyboard and go to the Attributes Inspector:
In my project I setup a Tab Bar Controller with 2 View Controllers and switch back and forth between the two. Seems to work OK for me.
Solved:
Swift 3.1
Just using this code in View Controller:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Swift 3
Set View controller-based status bar appearance in your project.plist to NO
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.setStatusBarStyle(.default, animated: animated)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.setStatusBarStyle(.lightContent, animated: animated)
}
An Objective-C
answer:
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
let color = UIColor(red:0.00, green:0.60, blue:0.48,alpha:1.0)
UINavigationBar.appearance().tintColor = UIColor.blue
UINavigationBar.appearance().barTintColor = color
OR
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
In swit4 this working fine in my project based on navigation bar
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height
let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
statusbarView.backgroundColor = UIColor.red
view.addSubview(statusbarView)
You can set status bar color using below code and its working for me
self.navigationController?.SetStatusBar(StatusBarbackgroundColor: ThemeBackColor, StatusTextColor: .black)
Thanks Happy Coding :)
Swift 5,iOS 14,UIKit
Step 1: Add Status bar style
Step 2: Change in Info.Plist
Add View controller-based status bar appearance key with NO value
Step 3: In your base view controller add this extension
extension UIViewController
{
func setStatusBarColor(){
if #available(iOS 13, *)
{
let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
let statusBarFrame = window?.windowScene?.statusBarManager?.statusBarFrame
let statusBar = UIView(frame: (statusBarFrame)!)
statusBar.backgroundColor = .orange
window?.addSubview(statusBar)
} else {
// ADD THE STATUS BAR AND SET A CUSTOM COLOR
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = .orange
}
UIApplication.shared.statusBarStyle = .lightContent
}
}
}
Step 4: In your view controller use this extension
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setStatusBarColor()
}
If you have derived your view controllers from a common view controller then, you can simply do it like this:
Step 1:
Add this key to your app's info.plist
file.
Step 2:
override
this in common view controller (or a ParentViewController
).
override var preferredStatusBarStyle: UIStatusBarStyle {
if self is YourChildViewController {
return .lightContent
}
return .default
}
That's it! No more fancy things.