我的应用程序背景是黑色的,但在iOS 7中,状态栏变成了透明的。所以我什么也看不见,只有角落里的绿色电池指示灯。如何将状态栏文本颜色改为白色,就像在主屏幕上一样?
当前回答
对我来说,什么都没用。我试图改变状态栏颜色在ViewController2,这是嵌入在NavigationController,这反过来,从ViewController1被模态地呈现。这种方法行不通:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .darkContent
}
什么都没有发生,直到我找到了这个解决方案: 将这一行添加到ViewController1
导航控制器。modalPresentationCapturesStatusBarAppearance = 真正的
let navigationController = UINavigationController(rootViewController: viewController2)
navigationController.modalPresentationStyle = .overFullScreen
navigationController.modalTransitionStyle = .crossDissolve
navigationController.modalPresentationCapturesStatusBarAppearance = true
self.present(navigationController, animated: true)
所以如果你的导航方案类似于ViewController1呈现的ViewController2,尝试modalPresentationCapturesStatusBarAppearance属性呈现的一个
文档:
The default value of this property is false. When you present a view controller by calling the present(_:animated:completion:) method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller's modalPresentationStyle value is UIModalPresentationStyle.fullScreen. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen. The system ignores this property’s value for a view controller presented fullscreen.
其他回答
这在iOS 7 UI过渡指南中有记录,你需要一个苹果开发者ID才能直接访问。相关节选:
因为状态栏是透明的,所以它后面的视图是透视的。[…使用UIStatusBarStyle常量来指定状态栏内容应该是暗的还是亮的: UIStatusBarStyleDefault显示黑色内容。[…] UIStatusBarStyleLightContent显示轻内容。当状态栏后面是黑色内容时使用。
也可能令人感兴趣:
在iOS 7中,你可以通过单个的vew控制器控制状态栏的样式,并在应用程序运行时更改它。要选择这种行为,添加UIViewControllerBasedStatusBarAppearance键到应用程序的信息。plist文件并赋值YES。
我强烈建议你浏览一下这个文档,同样,你可以用你的苹果开发者ID访问这个文档。
只需改变1)信息。plist基于控制器的状态栏外观->否 和写 2)
[[UIApplication
sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
以下是苹果关于状态栏更改的指导方针/说明。状态栏只允许显示暗&亮(而&黑)。
这里是-如何改变状态栏样式:
如果你想设置状态栏的风格,应用程序级别,然后设置UIViewControllerBasedStatusBarAppearance为NO在你的。plist文件。
如果你想在视图控制器级别设置状态栏样式,那么按照以下步骤:
如果你只需要在UIViewController级别设置状态栏样式,在.plist文件中将UIViewControllerBasedStatusBarAppearance设置为YES。 在viewDidLoad中添加函数setNeedsStatusBarAppearanceUpdate 重写视图控制器中的preferredStatusBarStyle。
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
根据状态栏样式设置级别设置.plist的值。
下面是一些在应用程序启动或视图控制器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
}
}
结果如下:
我认为所有的答案都不能真正指出问题,因为它们都适用于特定的场景。但如果你需要涵盖所有情况,请遵循以下几点:
根据你需要状态栏灯光样式的位置,你应该始终记住这3点:
1)如果你在启动屏幕或其他地方需要状态栏,而你无法控制它(不是在视图控制器中,而是在一些系统控制的元素/时刻,如启动屏幕) 进入项目设置
2)如果你在导航控制器中有一个控制器 你可以在接口构建器中修改它,如下所示:
a)选择导航控制器的导航栏
b)然后将导航栏的风格设置为“黑色”,因为这意味着你的状态栏下将有一个“黑色”->的黑色背景,所以它将把状态栏设置为白色
或者按照下面的代码来做
navigationController?.navigationBar.barStyle = UIBarStyle.Black
3)如果你有单独的控制器,需要有自己的状态栏风格,它不是作为UINavigationController嵌入在一些容器结构中
设置控制器的状态栏样式:
在信息。plist设置字段值NO查看基于控制器的状态栏外观,并在目标>常规设置中设置状态栏样式灯。
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- Ios模拟器:如何关闭应用程序
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- 如何舍入一个双到最近的Int在迅速?
- 扁平化数组的数组在Swift
- Swift:声明一个空字典
- 在成功提交我的应用程序后,“太多符号文件”
- 从数组中随机选择一个元素
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- CFNetwork SSLHandshake iOS 9失败