如何在iOS 7上更改状态栏背景颜色和文本颜色? Warif Akhand Rishi
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在iOS 7上更改状态栏背景颜色和文本颜色? Warif Akhand Rishi相关的知识,希望对你有一定的参考价值。
我目前的应用程序在ios 5和6上运行。
导航栏为橙色,状态栏为黑色背景颜色,白色为文本颜色。但是,当我在iOS 7上运行相同的应用程序时,我发现状态栏看起来是透明的,具有与导航栏相同的橙色背景颜色,状态栏文本颜色为黑色。
由于这个原因,我无法区分状态栏和导航栏。
如何使状态栏看起来与iOS 5和6中的状态相同,即黑色背景颜色和白色文本颜色?我该如何以编程方式执行此操作?
我不得不尝试寻找其他方法。哪个不涉及addSubview
在窗口。因为当键盘呈现时我正在向上移动窗口。
Objective-C
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
Swift
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
Swift 3
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
打电话给这个表格application:didFinishLaunchingWithOptions
为我工作。
注:我们在应用程序商店中有一个具有此逻辑的应用程序。所以我想应用商店政策是可以的。
编辑:
使用风险由您自己承担。组成评论者@Sebyddd
我有一个应用程序拒绝原因,而另一个被接受就好了。他们确实将其视为私有API使用,因此您在审核过程中会受到好运:) - Sebyddd
这是一个完整的,复制和粘贴的解决方案,带有
absolutely correct explanation
涉及的每个问题。
With thanks to Warif Akhand Rishi !
关于keyPath statusBarWindow.statusBar
的惊人发现。好的。
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
}
更改状态栏的背景颜色:Swift:
let proxyViewForStatusBar : UIView = UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20))
proxyViewForStatusBar.backgroundColor=UIColor.whiteColor()
self.view.addSubview(proxyViewForStatusBar)
斯威夫特4:
//更改状态栏背景颜色
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
对于iOS 9上的swift 2.0
将以下内容放在app委托中,位于didFinishLaunchingWithOptions下:
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)
iTroid23解决方案适合我。我错过了Swift解决方案。所以这可能有用:
1)在我的plist中,我不得不添加:
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
2)我不需要调用“setNeedsStatusBarAppearanceUpdate”。
3)在swift中我必须将它添加到我的UIViewController:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
我成功地自定义了StatusBar颜色非常简单,在方法中添加了AppDelegate.cs
文件:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
下一个代码:
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar!=null && statusBar.RespondsToSelector(new Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = Color.FromHex(RedColorHex).ToUIColor();
}
所以你得到这样的东西:
链接:https://jorgearamirez.wordpress.com/2016/07/18/lesson-x-effects-for-the-status-bar/
对于条形颜色:您为条形图提供自定义背景图像。
对于文本颜色:使用About Text Handling in iOS中的信息
如果你使用的是UINavigationController
,你可以使用这样的扩展名:
extension UINavigationController {
private struct AssociatedKeys {
static var navigationBarBackgroundViewName = "NavigationBarBackground"
}
var navigationBarBackgroundView: UIView? {
get {
return objc_getAssociatedObject(self,
&AssociatedKeys.navigationBarBackgroundViewName) as? UIView
}
set(newValue) {
objc_setAssociatedObject(self,
&AssociatedKeys.navigationBarBackgroundViewName,
newValue,
.OBJC_ASSOCIATION_RETAIN)
}
}
func setNavigationBar(hidden isHidden: Bool, animated: Bool = false) {
if animated {
UIView.a以上是关于如何在iOS 7上更改状态栏背景颜色和文本颜色? Warif Akhand Rishi的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:不使用AppBar时如何在Android和iOS上更改状态栏文本颜色[重复]