[英]How to change the status bar background color and text color on iOS 7?
[英]Change the status bar background color color past iOS 7
我想在iOS 7上更改状态栏的背景颜色,我正在使用此代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UIApplication sharedApplication].statusBarHidden = NO;
self.window.clipsToBounds = YES;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];
self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
...
...
}
当我写这个时,它会显示黑色背景的状态栏; 我希望它有一个红色背景。
如何更改状态栏的颜色为红色背景而不是黑色?
在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];
案例2:没有导航栏的视图
在这种情况下,请在viewDidLoad方法中使用以下代码
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 7及更高版本中,状态栏是透明的 。 将视图的backgroundColor
设置为状态栏所需的颜色。
或者,您可以在视图顶部添加一个20px高的红色子视图。
有关更多信息,请参阅Apple Transition Guide 。
另外,请确保您的preferredStatusBarStyle
是UIStatusBarStyleLightContent
。 并在您的Info.plist中将“查看基于控制器的状态栏外观”设置为“否”。
目标c
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setStatusBarHidden:false];
UIView *statusBar=[[UIApplication sharedApplication] valueForKey:@"statusBar"];
statusBar.backgroundColor = [UIColor redColor];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
return YES;
}
这对我有用。 我希望这对你也有帮助。
@Teja Kumar Bethina提供的内容很有帮助,但最好从UIApplication单例中获取statusBar的高度,如下所示:
UIApplication *app = [UIApplication sharedApplication];
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -app.statusBarFrame.size.height, self.view.bounds.size.width, app.statusBarFrame.size.height)];
statusBarView.backgroundColor = [UIColor blackColor];
在AppDelegate中使用
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
并在项目info.plist文件中
设置标志NO以在应用程序中查看基于控制器的状态栏外观。
从ViewDidLoad调用此函数并传递状态栏颜色。
func setStatusBarBackgroundColor(color: UIColor)
{
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView
else
{
return
}
statusBar.backgroundColor = color
}
希望它会有所帮助。
这是一个使用自动布局的快速解决方案,如果您的应用程序是纵向或横向,则条形将是正确的宽度。 它作为子视图添加到导航栏视图中,因此它偏移了-20。 您可以将其添加到导航栏的父视图中,然后不需要偏移。 此示例还将颜色设置为与导航栏相同。
希望有帮助:)
// Add colored opaque view behind the status bar view
let statusBarView = UIView()
statusBarView.backgroundColor = navigationBar.backgroundColor
statusBarView.translatesAutoresizingMaskIntoConstraints = false
navigationBar.addSubview(statusBarView)
let views = ["statusBarView": statusBarView]
let hConstraint = "H:|-0-[statusBarView]-0-|"
let vConstraint = "V:|-(-20)-[statusBarView(20)]"
var allConstraints = NSLayoutConstraint.constraintsWithVisualFormat(hConstraint,
options: [], metrics: nil, views: views)
allConstraints += NSLayoutConstraint.constraintsWithVisualFormat(vConstraint,
options: [], metrics: nil, views: views)
NSLayoutConstraint.activateConstraints(allConstraints)
如果您的应用程序是基于导航,那么您可以将状态栏背景颜色设置为红色,如下所
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
可能会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.