我的应用程序有一个黑色的背景,但在iOS 7中状态栏变成透明的了。因此我什么都看不到,只能看到角落里的绿色电池指示器。我该如何将状态栏文本颜色更改为像主屏幕上那样的白色呢?
我的应用程序有一个黑色的背景,但在iOS 7中状态栏变成透明的了。因此我什么都看不到,只能看到角落里的绿色电池指示器。我该如何将状态栏文本颜色更改为像主屏幕上那样的白色呢?
鉴于应用程序下方的情况,并且操作系统自动根据其自身修改状态栏样式。
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[application setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor blackColor];
}
我需要为Swift和导航控制器做些什么
extension UINavigationController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
window.rootViewController
,因为SceneDelegate
在SwiftUI生命周期中不存在。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中将“基于视图控制器的状态栏外观”设置为“是”。
- (UIViewController*) childViewControllerForStatusBarStyle
{
return self.viewControllers.lastObject;
}
在此之后,将调用推送的视图控制器上的preferredStatusBarStyle方法。
#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif
[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];
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,但我在许多项目中使用过它,而且苹果已经批准了。
只需在提交应用程序时,在评论部分将此代码发送给苹果,并告知您正在使用此代码以更改状态栏颜色。
是的,也不要忘记下面这个。
这里有一个更好的解决方案,扩展导航控制器并将其放入故事板中。
class NVC: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.isHidden = true
self.navigationController?.navigationBar.isTranslucent = false
self.navigationBar.barTintColor = UIColor.white
setStatusBarColor(view : self.view)
}
func setStatusBarColor(view : UIView){
if #available(iOS 13.0, *) {
let app = UIApplication.shared
let statusBarHeight: CGFloat = app.statusBarFrame.size.height
let statusbarView = UIView()
statusbarView.backgroundColor = UIColor.black
view.addSubview(statusbarView)
statusbarView.translatesAutoresizingMaskIntoConstraints = false
statusbarView.heightAnchor
.constraint(equalToConstant: statusBarHeight).isActive = true
statusbarView.widthAnchor
.constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
statusbarView.topAnchor
.constraint(equalTo: view.topAnchor).isActive = true
statusbarView.centerXAnchor
.constraint(equalTo: view.centerXAnchor).isActive = true
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.black
}
}
}
状态栏颜色将为黑色,文字将为白色
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :
[UIColor whiteColor],
NSFontAttributeName:[UIFont boldSystemFontOfSize:19]};