如何在iOS 7上更改状态栏背景颜色和文本颜色?
我当前的应用程序运行在iOS 5和iOS 6上 导航栏的颜色为橙色,状态栏的背景颜色为黑色,文本颜色为白色。但是,当我在iOS 7上运行相同的应用程序时,我观察到状态栏看起来是透明的,与导航栏的背景颜色相同,并且状态栏文本颜色为黑色 因此,我无法区分状态栏和导航栏如何在iOS 7上更改状态栏背景颜色和文本颜色?,ios,ios7,uicolor,ios-statusbar,Ios,Ios7,Uicolor,Ios Statusbar,我当前的应用程序运行在iOS 5和iOS 6上 导航栏的颜色为橙色,状态栏的背景颜色为黑色,文本颜色为白色。但是,当我在iOS 7上运行相同的应用程序时,我观察到状态栏看起来是透明的,与导航栏的背景颜色相同,并且状态栏文本颜色为黑色 因此,我无法区分状态栏和导航栏 如何使状态栏与iOS 5和iOS 6中的状态栏相同,即黑色背景色和白色文本色?如何通过编程实现这一点?在iOS 7中,状态栏没有背景,因此如果在其后面放置一个黑色的20px高视图,将获得与iOS 6相同的结果 此外,您可能需要阅读以了
如何使状态栏与iOS 5和iOS 6中的状态栏相同,即黑色背景色和白色文本色?如何通过编程实现这一点?在iOS 7中,状态栏没有背景,因此如果在其后面放置一个黑色的20px高视图,将获得与iOS 6相同的结果 此外,您可能需要阅读以了解有关主题的更多信息。1)在plist中将UIViewControllerBasedStatusBarAppearance设置为YES 2) 在viewDidLoad中执行
[self-setNeedsStatusBarAppearanceUpdate]代码>
3) 添加以下方法:
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
更新:
还要检查条形图颜色:为条形图提供自定义背景图像
对于文本颜色:使用中的信息在ViewDidLoad方法中写入:
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
self.edgesForExtendedLayout=UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;
}
它在一定程度上修复了我的状态栏颜色和其他UI错误位置。对于背景,您可以轻松添加视图,如示例所示:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
[navbar addSubview:view];
其中“导航栏”是UINavigationBar。转到应用程序info.plist
1) 将查看基于控制器的状态栏外观
设置为否
2) 将状态栏样式设置为UIStatusBarStyleLightContent
然后转到应用程序代理并将以下代码粘贴到设置Windows的RootViewController的位置。
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
view.backgroundColor=[UIColor blackColor];
[self.window.rootViewController.view addSubview:view];
}
希望能有所帮助。只需补充Shahid的答案-您可以使用以下(iOS7+)说明方向变化或不同的设备:
在iOS 7中处理状态栏的背景色时,有两种情况
案例1:使用导航栏查看
在这种情况下,请在viewDidLoad方法中使用以下代码
UIApplication *app = [UIApplication sharedApplication];
CGFloat statusBarHeight = app.statusBarFrame.size.height;
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
statusBarView.backgroundColor = [UIColor yellowColor];
[self.navigationController.navigationBar addSubview:statusBarView];
UIApplication *app = [UIApplication sharedApplication];
CGFloat statusBarHeight = app.statusBarFrame.size.height;
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
statusBarView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:statusBarView];
案例2:不带导航栏的视图
在这种情况下,请在viewDidLoad方法中使用以下代码
UIApplication *app = [UIApplication sharedApplication];
CGFloat statusBarHeight = app.statusBarFrame.size.height;
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
statusBarView.backgroundColor = [UIColor yellowColor];
[self.navigationController.navigationBar addSubview:statusBarView];
UIApplication *app = [UIApplication sharedApplication];
CGFloat statusBarHeight = app.statusBarFrame.size.height;
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
statusBarView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:statusBarView];
源链接更改状态栏的背景颜色:
斯威夫特:
对于iOS 9上的swift 2.0
将以下内容放置在应用程序代理中的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)
这个解决方案对我有效。我错过了快速解决方案。因此,这可能会有所帮助:
1) 在我的列表中,我必须添加以下内容:
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
警告:它不再适用于iOS 13和Xcode 11。
========================================================================
我不得不试着寻找其他的方法。它不涉及窗口上的addSubview
。因为当键盘出现时,我正在向上移动窗口
目标-C
敏捷的
斯威夫特3
调用此表单应用程序:didFinishLaunchingWithOptions
对我有效
注意:我们在应用商店中有一个应用程序使用了这种逻辑。所以我想应用商店的政策是可以接受的
编辑:
使用风险自负。在sebydd处形成评论
我有一个应用程序因为这个原因被拒绝了,而另一个应用程序刚刚被接受
好的他们认为它是私有的API使用,所以你必须服从。
审查过程中的运气:)–Sebyddd
这是一个完整的复制粘贴解决方案
绝对正确的解释
涉及到的每一个问题
谢谢你!
有关keyPathstatusBarWindow.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
}
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptions:[UIApplicationLaunchOptions:Any]?)->Bool{
//处理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.animate(withDuration: 0.3) {
self.navigationBarBackgroundView?.isHidden = isHidden
}
} else {
navigationBarBackgroundView?.isHidden = isHidden
}
}
func setNavigationBarBackground(color: UIColor, includingStatusBar: Bool = true, animated: Bool = false) {
navigationBarBackgroundView?.backgroundColor = UIColor.clear
navigationBar.backgroundColor = UIColor.clear
navigationBar.barTintColor = UIColor.clear
let setupOperation = {
if includingStatusBar {
self.navigationBarBackgroundView?.isHidden = false
if self.navigationBarBackgroundView == nil {
self.setupBackgroundView()
}
self.navigationBarBackgroundView?.backgroundColor = color
} else {
self.navigationBarBackgroundView?.isHidden = true
self.navigationBar.backgroundColor = color
}
}
if animated {
UIView.animate(withDuration: 0.3) {
setupOperation()
}
} else {
setupOperation()
}
}
private func setupBackgroundView() {
var frame = navigationBar.frame
frame.origin.y = 0
frame.size.height = 64
navigationBarBackgroundView = UIView(frame: frame)
navigationBarBackgroundView?.translatesAutoresizingMaskIntoConstraints = true
navigationBarBackgroundView?.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
navigationBarBackgroundView?.isUserInteractionEnabled = false
view.insertSubview(navigationBarBackgroundView!, aboveSubview: navigationBar)
}
}
它基本上使导航栏背景透明,并使用另一个UIView作为背景。您可以调用导航控制器的setNavigationBarBackground
方法来设置导航栏背景颜色和状态栏
请记住,然后必须使用setNavigationBar(隐藏:Bool,动画:Bool)
方法,否则用作背景的视图仍将可见。您可以在应用程序启动或视图控制器加载期间为状态栏设置背景色
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
}
}
结果如下:
这是关于状态栏的更改。状态栏中只允许黑暗和光明(而黑色和黑色)
以下是-如何更改状态栏样式:
如果要设置状态栏样式,请在应用程序级别将“.plist”文件中的UIViewControllerBasedStatusBarAppearance
设置为NO
如果要在视图控制器级别设置状态栏样式,请执行以下步骤:
如果只需要在UIViewController级别设置状态栏样式,请在.plist
文件中将UIViewControllerBasedStatusBarAppearance
设置为YES
在viewDidLoad添加函数中-SetNeedssStatusBarAppearanceUpdate
覆盖视图控制器中的preferredStatusBarStyle
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
}
}
-
试试这个。
在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应用程序、NSDictionary选项)
下一个代码:
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar!=null && statusBar.RespondsToSelector(new Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = Color.FromHex(RedColorHex).ToUIColor();
}
你会得到这样的结果:
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.animate(withDuration: 0.3) {
self.navigationBarBackgroundView?.isHidden = isHidden
}
} else {
navigationBarBackgroundView?.isHidden = isHidden
}
}
func setNavigationBarBackground(color: UIColor, includingStatusBar: Bool = true, animated: Bool = false) {
navigationBarBackgroundView?.backgroundColor = UIColor.clear
navigationBar.backgroundColor = UIColor.clear
navigationBar.barTintColor = UIColor.clear
let setupOperation = {
if includingStatusBar {
self.navigationBarBackgroundView?.isHidden = false
if self.navigationBarBackgroundView == nil {
self.setupBackgroundView()
}
self.navigationBarBackgroundView?.backgroundColor = color
} else {
self.navigationBarBackgroundView?.isHidden = true
self.navigationBar.backgroundColor = color
}
}
if animated {
UIView.animate(withDuration: 0.3) {
setupOperation()
}
} else {
setupOperation()
}
}
private func setupBackgroundView() {
var frame = navigationBar.frame
frame.origin.y = 0
frame.size.height = 64
navigationBarBackgroundView = UIView(frame: frame)
navigationBarBackgroundView?.translatesAutoresizingMaskIntoConstraints = true
navigationBarBackgroundView?.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
navigationBarBackgroundView?.isUserInteractionEnabled = false
view.insertSubview(navigationBarBackgroundView!, aboveSubview: navigationBar)
}
}
链接:Swift 4<
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[application setStatusBarHidden:NO];
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor blackColor];
}
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar!=null && statusBar.RespondsToSelector(new Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = Color.FromHex(RedColorHex).ToUIColor();
}
UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = .lightContent
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
//Set status bar background colour
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
//Set navigation bar subView background colour
for view in controller.navigationController?.navigationBar.subviews ?? [] {
view.tintColor = UIColor.white
view.backgroundColor = UIColor.red
}
})
let statusBarView = UIView(frame: CGRect(x: 0, y: 0, width: view.width, height: 20.0))
statusBarView.backgroundColor = UIColor.red
self.navigationController?.view.addSubview(statusBarView)
if (@available(iOS 13.0, *)) {
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
statusBar.backgroundColor = [UIColor whiteColor];
[[UIApplication sharedApplication].keyWindow addSubview:statusBar];
} else {
// Fallback on earlier versions
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor whiteColor];//set whatever color you like
}
}
extension UIApplication {
var statusBarView: UIView? {
if #available(iOS 13.0, *) {
let statusBar = UIView()
statusBar.frame = UIApplication.shared.statusBarFrame
UIApplication.shared.keyWindow?.addSubview(statusBar)
return statusBar
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
return statusBar
}
}
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
extension UINavigationController {
func setStatusBar(backgroundColor: UIColor) {
let statusBarFrame: CGRect
if #available(iOS 13.0, *) {
statusBarFrame = view.window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero
} else {
statusBarFrame = UIApplication.shared.statusBarFrame
}
let statusBarView = UIView(frame: statusBarFrame)
statusBarView.backgroundColor = backgroundColor
view.addSubview(statusBarView)
}
}