xcode 优选状态栏样式不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45372550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
preferredStatusBarStyle is not working
提问by Sarah
I used to use setStatusBarStyle
in my project and it works fine, but it is deprecated so I use preferredStatusBarStyle
, that didn't work.
knowing that I've:
我曾经setStatusBarStyle
在我的项目中使用它并且它工作正常,但它已被弃用,所以我使用preferredStatusBarStyle
,这不起作用。知道我已经:
- Call the method setNeedsStatusBarAppearanceUpdate.
- Set "View controller-based status bar appearance" to NO in info.plist
Override the function
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
this function is not called
- 调用方法 setNeedsStatusBarAppearanceUpdate。
- 在 info.plist 中将“基于控制器的状态栏外观”设置为 NO
覆盖函数
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
这个函数没有被调用
Note: I'm using navigation controller.
注意:我正在使用导航控制器。
回答by Krunal
Here is Apple Guidelines/Instructionabout status bar change.
这是有关状态栏更改的Apple 指南/说明。
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance
to NO
in your .plist
file. And in your appdelegate
> didFinishLaunchingWithOptions
add following ine (programatically you can do it from app delegate).
如果你想设置状态栏样式,应用的水平,那么设置UIViewControllerBasedStatusBarAppearance
到NO
您的.plist
文件。并在您的appdelegate
>didFinishLaunchingWithOptions
添加以下ine(以编程方式,您可以从应用程序委托中完成)。
Objective C
目标C
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
Swift
迅速
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}
if you want to set status bar style, at view controller level then follow these steps:
如果要设置状态栏样式,请在视图控制器级别执行以下步骤:
- Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the.plist
file, if you need to set status bar style at UIViewController level only. In the viewDidLoad add function -
setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
- 设置
UIViewControllerBasedStatusBarAppearance
到YES
中.plist
文件,如果需要设置状态栏样式仅UIViewController的水平。 在 viewDidLoad 添加函数 -
setNeedsStatusBarAppearanceUpdate
覆盖视图控制器中的 preferredStatusBarStyle。
Objective C
目标C
- (void)viewDidLoad
{
[super viewDidLoad];
[self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
Swift
迅速
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Set value of .plist according to status bar style setup level.
根据状态栏样式设置级别设置 .plist 的值。
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
您可以在应用程序启动期间或视图控制器的 viewDidLoad 期间设置状态栏的背景颜色。
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
这是结果: