状态栏backgroundColor

时间:2013-12-24 11:56:56

标签: ios iphone ios7 statusbar

我正在使用ios 7我想设置状态栏背景图像。 我已经做到了这一点,但它仍然没有改变任何东西:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    UIView *addStatusBar = [[UIView alloc] init];
    addStatusBar.frame = CGRectMake(0, 0, 320, 20);
    addStatusBar.backgroundColor = [UIColor redColor]; //change this to match your navigation bar
    [self.window.rootViewController.view addSubview:addStatusBar];
}

3 个答案:

答案 0 :(得分:1)

我这样做就像.h文件

@property (retain, nonatomic) UIWindow *statusBarBackground;

和.m文件

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    self.statusBarBackground = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, self.window.frame.size.width, 20)];
    self.statusBarBackground.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"statusbar_bg"]];
    [self.statusBarBackground makeKeyAndVisible];
}

将此添加到您的控制器

- (void) viewDidLayoutSubviews {
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        CGRect viewBounds = self.view.bounds;
        if (viewBounds.origin.y == 0) {
            CGFloat topBarOffset = self.topLayoutGuide.length;
            viewBounds.origin.y -= topBarOffset;
            self.view.bounds = viewBounds;
        }
    }
}

答案 1 :(得分:0)

你必须做两件事。

(1)打开info.plist并设置"View controller-based status bar appearance" = NO

(2)将这些行添加到application:didFinishLaunchingWithOptions

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{   
    self.window.clipsToBounds = YES;
    [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];     

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        self.window.frame =  CGRectMake(20, 0,self.window.frame.size.width-20,self.window.frame.size.height);
        self.window.bounds = CGRectMake(20, 0, self.window.frame.size.width, self.window.frame.size.height);
    } else
    {
        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);
    }
}

答案 2 :(得分:0)

您的代码有效,但您必须稍微修改一下。这是它应该是什么样的..

// 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 colorWithPatternImage:[UIImage imageNamed:@"bg1.png"]];
//here you are adding the image as the background image
    [self.window.rootViewController.view addSubview:addStatusBar];

不要忘记将图像导入项目。现在我刚刚在app delegate的应用程序didfinishwithoptions中插入了上面的代码,但如果你想使用相同的不同视图,你应该可以使用相同的代码。 enter image description here