我遵循这个线程重写-preferredStatusBarStyle,但它没有被调用。 有什么选项我可以改变来启用它吗?(我在我的项目中使用xib。)
当前回答
在Swift中,对于任何类型的UIViewController:
在你的AppDelegate集合中:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window!.rootViewController = myRootController
return true
}
myRootController可以是任何类型的UIViewController,例如UITabBarController或UINavigationController。
然后,像这样重写这个根控制器:
class RootController: UIViewController {
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
}
这将改变状态栏在整个应用程序中的外观,因为根控制器只负责状态栏的外观。
记住在你的信息中将基于视图控制器的状态栏外观属性设置为YES。Plist使其工作(这是默认值)。
其他回答
除了serenn的回答,如果你用modalPresentationStyle(例如.overCurrentContext)呈现一个视图控制器,你也应该在新呈现的视图控制器上调用这个:
presentedViewController.modalPresentationCapturesStatusBarAppearance = true
不要忘记在呈现的视图控制器中重写preferredStatusBarStyle。
iOS 7中的UIStatusBarStyle
iOS 7的状态栏是透明的,它后面的视图是透视的。
状态栏的样式是指其内容的外观。在ios7中,状态栏内容要么是暗的(UIStatusBarStyleDefault),要么是亮的(UIStatusBarStyleLightContent)。uistatusbarstyleblack半透明和UIStatusBarStyleBlackOpaque在iOS 7.0中已弃用。使用UIStatusBarStyleLightContent代替。
如何改变UIStatusBarStyle
如果状态栏下面是一个导航栏,状态栏的样式将被调整以匹配导航栏的样式(UINavigationBar.barStyle):
具体来说,如果导航栏样式是UIBarStyleDefault,状态栏样式将是UIStatusBarStyleDefault;如果导航栏样式是UIBarStyleBlack,状态栏样式将是UIStatusBarStyleLightContent。
如果状态栏下面没有导航栏,状态栏样式可以在应用程序运行时由单个视图控制器控制和更改。
-[UIViewController preferredStatusBarStyle]是ios7新增的方法。它可以被重写以返回首选的状态栏样式:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
如果状态栏样式应该由子视图控制器控制,而不是由self控制,重写-[UIViewController childViewControllerForStatusBarStyle]以返回子视图控制器。
如果你想选择退出这个行为,并通过使用-[UIApplication statusBarStyle]方法设置状态栏样式,添加UIViewControllerBasedStatusBarAppearance键到应用程序的信息。plist文件并给它赋值NO。
Hippo的回答:如果你正在使用UINavigationController,那么最好添加一个类别:
// UINavigationController+StatusBarStyle.h:
@interface UINavigationController (StatusBarStyle)
@end
// UINavigationController+StatusBarStyle.m:
@implementation UINavigationController (StatusBarStyle)
- (UIStatusBarStyle)preferredStatusBarStyle
{
//also you may add any fancy condition-based code here
return UIStatusBarStyleLightContent;
}
@end
这种解决方案可能比切换到即将被弃用的行为要好。
对于任何使用UINavigationController的人:
UINavigationController不会转发preferredStatusBarStyle调用给它的子视图控制器。相反,它管理自己的状态——就像它应该做的那样,它在屏幕顶部绘制状态栏,因此应该对状态栏负责。因此,在导航控制器中实现preferredStatusBarStyle在vc中什么都不会做——它们永远不会被调用。
诀窍是UINavigationController使用什么来决定UIStatusBarStyleDefault或uistatusbarstyelightcontent返回什么。它基于它的UINavigationBar.barStyle。默认的(UIBarStyleDefault)导致前景UIStatusBarStyleDefault状态栏为黑色。UIBarStyleBlack会给出一个uistatusbarstyelightcontent状态栏。
TL; diana:
如果你想在UINavigationController上使用UIStatusBarStyleLightContent:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
Swift 4.2及以上版本
正如所选答案中提到的,根本原因是检查您的窗口根视图控制器对象。
流结构的可能情况
Custom UIViewController object is window root view controller Your window root view controller is a UIViewController object and it further adds or removes navigation controller or tabController based on your application flow. This kind of flow is usually used if your app has pre login flow on navigation stack without tabs and post login flow with tabs and possibly every tab further holds navigation controller. TabBarController object is window root view controller This is the flow where window root view controller is tabBarController possibly every tab further holds navigation controller. NavigationController object is window root view controller This is the flow where window root view controller is navigationController. I am not sure if there is any possibility to add tab bar controller or new navigation controller in an existing navigation controller. But if there is such case, we need to pass the status bar style control to the next container. So, I added the same check in UINavigationController extension to find childForStatusBarStyle
使用以下扩展,它处理所有上述场景-
extension UITabBarController {
open override var childForStatusBarStyle: UIViewController? {
return selectedViewController?.childForStatusBarStyle ?? selectedViewController
}
}
extension UINavigationController {
open override var childForStatusBarStyle: UIViewController? {
return topViewController?.childForStatusBarStyle ?? topViewController
}
}
extension AppRootViewController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
return children.first { $0.childForStatusBarStyle != nil }?.childForStatusBarStyle?.preferredStatusBarStyle ?? .default
}
}
在info中你不需要UIViewControllerBasedStatusBarAppearance键。Plist默认为true
对于更复杂的流程,需要考虑的要点
In case you present new flow modally, it detaches from the existing status bar style flow. So, suppose you are presenting a NewFlowUIViewController and then add new navigation or tabBar controller to NewFlowUIViewController, then add extension of NewFlowUIViewController as well to manage further view controller's status bar style. In case you set modalPresentationStyle other than fullScreen while presenting modally, you must set modalPresentationCapturesStatusBarAppearance to true so that presented view controller must receive status bar appearance control.
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- Ios模拟器:如何关闭应用程序
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- Swift:声明一个空字典
- 在成功提交我的应用程序后,“太多符号文件”
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue