iOS 7状态栏背景 [英] iOS 7 status bar background
问题描述
我有一些顶部有工具栏的视图控制器,看起来像这样
I have some view controllers with a toolbar at top and look like this
如何填充状态栏背景以匹配工具栏背景以匹配新的iOS 7风格?
How I can fill the status bar background to match the toolbar background so its match the new iOS 7 style?
推荐答案
您需要在应用程序委托中添加一个子视图,并根据您的喜好更改颜色。这里是位于 applicationdidfinishLaunchingWithOptions
you need to add a subview in app delegate and change the color to your liking. here is a sample of the code that sits in applicationdidfinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
//change this to match your navigation bar or view color or tool bar
//You can also use addStatusBar.backgroundColor = [UIColor BlueColor]; or any other color
addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
[self.window.rootViewController.view addSubview:addStatusBar];
}
return YES;
}
查看评论。您可以使用 addStatusBar.backGroundColor
中的任何类型的颜色来匹配所需的颜色。注意,这里的红色只是产生一个黑色背景,改变到任何你需要的。对于深灰色,用以下代码替换:
take a look at the comments. you can use any type of color in addStatusBar.backGroundColor
to match the color you need. note that the color with red here is just producing a Black background, change that to whatever you need. for dark grey replace that with the following code:
addStatusBar.backgroundColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1];
编辑:
你可以在之后的
ViewDidLoad
方法中插入以下代码(它将访问您要更改的视图):super viewDidLoad ];
for changing the status bar color in individual views you simply plug in the following code ( which will access the view you want to change) in ViewDidLoad
method right after [super viewDidLoad];
that should change the color of the status bar in just that view you place the code in.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
[self.view addSubview:addStatusBar];
edit1:
要在导航控制器的nag栏顶部添加一个子视图,您必须将子视图的位置设置为两倍,如下所示:
in case of trying to add a subview on top of the nag bar in navigation controller, you have to twice the position of the subview, something like the following:
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, -20, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:127.0/255. green:0.0/255. blue:127.0/255. alpha:1];
[self.view addSubview:addStatusBar];
[self.navigationController.navigationBar addSubview:addStatusBar];
,还需要将子视图添加到导航控制器导航栏的子视图。我将子视图的背景颜色设置为紫色,但您可以更改。
and also you need to add your subview as yet the subview of the navigation controllers' navigation bar. i set the background color of the subview to purple but you can change that.
这篇关于iOS 7状态栏背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!