我的应用程序背景是黑色的,但在iOS 7中,状态栏变成了透明的。所以我什么也看不见,只有角落里的绿色电池指示灯。如何将状态栏文本颜色改为白色,就像在主屏幕上一样?
当前回答
我做了一些不同的事情,这对我很有效。
在没有更改代码的情况下,我像这样配置了.plist文件:
基于控制器的状态栏外观>否 状态栏样式> UIStatusBarStyleLightContent(简单字符串)
我希望这能有所帮助。
edit
对于每个视图控制器,我改变“状态栏”的模拟度量属性,在故事板中,从“推断”到“轻内容”
其他回答
如果你的UIViewController在UINavigationController中,你必须设置BarStyle:
-[UINavigationBar setBarStyle:UIBarStyleBlack]
原始答案在这里
https://devforums.apple.com/message/844264#844264
如果你想将它设置为任何颜色,请使用下面的代码。
id statusBarWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
id statusBar = [statusBarWindow valueForKey:@"statusBar"];
SEL setForegroundColor_sel = NSSelectorFromString(@"setForegroundColor:");
if([statusBar respondsToSelector:setForegroundColor_sel]) {
// iOS 7+
[statusBar performSelector:setForegroundColor_sel withObject:YourColorHere];
^^^^^^^^^^^^^
}
我知道我正在访问私有API,但我已经在许多项目中使用了这个,苹果已经批准了它。
在提交应用程序时,将此代码发送到Apple的评论部分,并告知您正在使用此代码更改状态栏的颜色。
是的,不要忘记下面的内容。
不需要做一些额外的,只是写这段代码在你的viewController和获得状态栏颜色白色
- (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;}
你可以在iOS 6和7中使用这个:
#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif
[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];
这个解决方案适用于使用新的SwiftUI Lifecycle / iOS 14.0的应用程序:
我需要动态更改状态栏文本颜色,无法访问窗口。rootViewController,因为SwiftUI生命周期中不存在SceneDelegate。
我终于找到了Xavier Donnellon的简单解决方案:https://github.com/xavierdonnellon/swiftui-statusbarstyle
复制StatusBarController.swift文件到你的项目中,并将你的主视图包装成一个RootView:
@main
struct ProjectApp: App {
var body: some Scene {
WindowGroup {
//wrap main view in RootView
RootView {
//Put the view you want your app to present here
ContentView()
//add necessary environment objects here
}
}
}
}
然后你可以通过使用。statusbarstyle (. darkcontent)或。statusbarstyle (. lightcontent)视图修饰符来改变状态栏文本的颜色,或者直接调用例如UIApplication.setStatusBarStyle(. lightcontent)。
不要忘记在Info.plist中设置“基于视图控制器的状态栏外观”为“YES”。
推荐文章
- 如何删除默认的导航栏空间在SwiftUI导航视图
- Ios模拟器:如何关闭应用程序
- 如何在iOS中使用Swift编程segue
- Swift -整数转换为小时/分钟/秒
- 如何舍入一个双到最近的Int在迅速?
- 扁平化数组的数组在Swift
- Swift:声明一个空字典
- 在成功提交我的应用程序后,“太多符号文件”
- 从数组中随机选择一个元素
- 首先添加一个UIView,甚至是导航栏
- 我如何改变UIButton标题颜色?
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- CFNetwork SSLHandshake iOS 9失败