首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 7状态栏背景

iOS 7状态栏背景
EN

Stack Overflow用户
提问于 2013-12-22 11:37:14
回答 1查看 6.2K关注 0票数 1

我有一些视图控制器,顶部有一个工具栏,如下所示

如何填充状态栏背景以匹配工具栏背景,从而使其与新的iOS 7样式匹配?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-22 15:34:53

您需要在app委托中添加一个子视图,并根据您的喜好更改颜色。下面是位于applicationdidfinishLaunchingWithOptions中的代码示例

代码语言:javascript
复制
- (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中使用任何类型的颜色来匹配所需的颜色。请注意,这里红色的颜色只是产生一个黑色的背景,将其更改为您需要的任何东西。对于深灰色,请用以下代码替换:

代码语言:javascript
复制
addStatusBar.backgroundColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1];

编辑:

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

代码语言:javascript
复制
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栏顶部添加子视图,则必须将子视图的位置提高一倍,如下所示:

代码语言:javascript
复制
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];

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

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20728970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档