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
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// handle the iOS bar!
// >>>>>NOTE<<<<<
// >>>>>NOTE<<<<<
// >>>>>NOTE<<<<<
// "Status Bar Style" refers to the >>>>>color of the TEXT<<<<<< of the Apple status bar,
// it does NOT refer to the background color of the bar. This causes a lot of confusion.
// >>>>>NOTE<<<<<
// >>>>>NOTE<<<<<
// >>>>>NOTE<<<<<
// our app is white, so we want the Apple bar to be white (with, obviously, black writing)
// make the ultimate window of OUR app actually start only BELOW Apple's bar....
// so, in storyboard, never think about the issue. design to the full height in storyboard.
let h = UIApplication.shared.statusBarFrame.size.height
let f = self.window?.frame
self.window?.frame = CGRect(x: 0, y: h, width: f!.size.width, height: f!.size.height - h)
// next, in your plist be sure to have this: you almost always want this anyway:
// <key>UIViewControllerBasedStatusBarAppearance</key>
// <false/>
// next - very simply in the app Target, select "Status Bar Style" to Default.
// Do nothing in the plist regarding "Status Bar Style" - in modern Xcode, setting
// the "Status Bar Style" toggle simply sets the plist for you.
// finally, method A:
// set the bg of the Apple bar to white. Technique courtesy Warif Akhand Rishi.
// note: self.window?.clipsToBounds = true-or-false, makes no difference in method A.
if let sb = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
sb.backgroundColor = UIColor.white
// if you prefer a light gray under there...
//sb.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.9, alpha: 1)
}
/*
// if you prefer or if necessary, method B:
// explicitly actually add a background, in our app, to sit behind the apple bar....
self.window?.clipsToBounds = false // MUST be false if you use this approach
let whiteness = UIView()
whiteness.frame = CGRect(x: 0, y: -h, width: f!.size.width, height: h)
whiteness.backgroundColor = UIColor.green
self.window!.addSubview(whiteness)
*/
return true
}
let view: UIView = UIView.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, 20))
view.backgroundColor = UIColor.blackColor() //The colour you want to set
view.alpha = 0.1 //This and the line above is set like this just if you want
the status bar a darker shade of
the colour you already have behind it.
self.window!.rootViewController!.view.addSubview(view)
19 回答
我不得不尝试寻找其他方法 . 哪个不涉及
addSubview
在窗口 . 因为当键盘出现时我正在向上移动窗口 .目标C.
斯威夫特
斯威夫特3
拨打此表单
application:didFinishLaunchingWithOptions
为我工作 .注:我们在应用程序商店中有一个具有此逻辑的应用程序 . 所以我想应用商店政策是可以的 .
Edit:
使用风险由您自己承担 . 组成评论者@Sebyddd
转到你的应用
info.plist
1)将
View controller-based status bar appearance
设为NO
2)将
Status bar style
设为UIStatusBarStyleLightContent
然后转到您的应用程序委托并将以下代码粘贴到您设置Windows的RootViewController的位置 .
希望能帮助到你 .
1)在plist中将UIViewControllerBasedStatusBarAppearance设置为YES
2)在viewDidLoad中做
[self setNeedsStatusBarAppearanceUpdate];
3)添加以下方法:
更新:
还检查developers-guide-to-the-ios-7-status-bar
在iOS 7中处理状态栏的背景颜色时,有两种情况
案例1:使用导航栏查看
在这种情况下,请在viewDidLoad方法中使用以下代码
案例2:没有导航栏的视图
在这种情况下,请在viewDidLoad方法中使用以下代码
来源链接http://code-ios.blogspot.in/2014/08/how-to-change-background-color-of.html
在iOS 7中,状态栏没有背景,因此如果您在其后面放置一个黑色的20px高视图,您将获得与iOS 6相同的结果 .
另外,您可能需要阅读iOS 7 UI Transition Guide以获取有关该主题的更多信息 .
您可以在应用程序启动期间或视图控制器的viewDidLoad期间为状态栏设置背景颜色 .
结果如下:
这是关于状态栏更改的Apple Guidelines/Instruction . 状态栏中只允许黑暗和浅色(黑色和黑色) .
这是 - 如何更改状态栏样式:
如果要设置状态栏样式,应用程序级别,然后在
.plist'文件中将
UIViewControllerBasedStatusBarAppearance设置为
NO` .如果您要设置状态栏样式,请在视图控制器级别,然后按照下列步骤操作:
如果只需要在UIViewController级别设置状态栏样式,请在
.plist
文件中将UIViewControllerBasedStatusBarAppearance
设置为YES
.在viewDidLoad中添加功能 -
setNeedsStatusBarAppearanceUpdate
覆盖视图控制器中的preferredStatusBarStyle .
在ViewDidLoad方法中写下这个:
它在一定程度上修复了我和其他UI错位的状态栏颜色 .
只是为了添加Shahid的答案 - 您可以使用此方法考虑方向更改或不同的设备(iOS7):
对于背景,您可以轻松添加视图,例如:
其中“navbar”是UINavigationBar .
我希望它可以帮到你!
这是一个完整的,复制和粘贴的解决方案,带有
绝对正确的解释
涉及的每个问题 .
感谢Warif Akhand Rishi!
关于keyPath
statusBarWindow.statusBar
的惊人发现 . 好的 .更改状态栏的背景颜色:Swift:
对于iOS 9上的swift 2.0
将以下内容放在app委托中,在didFinishLaunchingWithOptions下:
iTroid23解决方案适合我 . 我错过了Swift解决方案 . 所以这可能有用:
1)在我的plist中,我不得不添加:
2)我不需要调用“setNeedsStatusBarAppearanceUpdate” .
3)在swift中我必须将它添加到我的UIViewController中:
对于条形颜色:您为条形图提供自定义背景图像 .
对于文本颜色:使用About Text Handling in iOS中的信息
如果你使用的是
UINavigationController
,你可以使用这样的扩展名:它基本上使导航栏背景透明,并使用另一个UIView作为背景 . 您可以调用导航控制器的
setNavigationBarBackground
方法,将导航栏背景颜色与状态栏一起设置 .请记住,如果要隐藏导航栏,则必须在扩展中使用
setNavigationBar(hidden: Bool, animated: Bool)
方法,否则用作背景的视图仍然可见 .请试试这个 . 使用这段代码在你的appdelegate类“didFinishLaunchingWithOptions”函数中
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [application setStatusBarHidden:NO]; UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = [UIColor blackColor]; }
我成功地自定义了StatusBar颜色非常简单,在方法中添加
AppDelegate.cs
文件:public override bool FinishedLaunching(UIApplication app, NSDictionary options)
下一个代码:
所以你得到这样的东西:
链接:https://jorgearamirez.wordpress.com/2016/07/18/lesson-x-effects-for-the-status-bar/
Swift 4
在
Info.plist
中添加此属性View controller-based status bar appearance to NO
然后在
AppDelegate
里面didFinishLaunchingWithOptions
添加这些代码行Swift 4:-
//更改状态栏背景颜色