iOS 7状态栏背景

时间:2013-12-22 11:37:14

标签: objective-c cocoa-touch ios7

我有一些顶部有工具栏的视图控制器,看起来像enter image description here

如何填充状态栏背景以匹配工具栏背景,使其与新的iOS 7风格相匹配?

1 个答案:

答案 0 :(得分:8)

您需要在app delegate中添加子视图并根据自己的喜好更改颜色。以下是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中使用任何类型的颜色来匹配您需要的颜色。请注意,此处带红色的颜色只是产生黑色背景,将其更改为您需要的任何颜色。对于深灰色,请使用以下代码替换它:

addStatusBar.backgroundColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1];

编辑:

要更改各个视图中的状态栏颜色,只需在ViewDidLoad之后的[super viewDidLoad];方法中插入以下代码(将访问您要更改的视图),该代码应更改颜色只需在该视图中显示代码的状态栏。

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:

如果尝试在导航控制器的唠叨栏顶部添加子视图,则必须将子视图的位置增加两倍,如下所示:

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];

并且您还需要将子视图添加为导航控制器导航栏的子视图。我将子视图的背景颜色设置为紫色,但您可以更改它。