vivek bhoraniya
vivek bhoraniya

Reputation: 1535

Status bar backgroundColor

I am using ios 7 I want to set stauts bar background image. I have done this but still it is not changing anything:

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

Upvotes: 3

Views: 451

Answers (3)

Adrian P
Adrian P

Reputation: 6529

your code works, but you have to modify it a bit. here is what it should look like..

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

don't forget to import your image to the project. now i have just plugged in the above code in application didfinishwithoptions of the app delegate, but you should be able to use the same if you want different views using the same. enter image description here

Upvotes: 0

Retro
Retro

Reputation: 4005

I have done this like .h file

@property (retain, nonatomic) UIWindow *statusBarBackground;

and in .m file

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

add this to your controllers

- (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;
        }
    }
}

Upvotes: 1

Pradhyuman sinh
Pradhyuman sinh

Reputation: 3928

You have to do 2 things.

(1) Open your info.plist and set "View controller-based status bar appearance" = NO

(2) add these lines to 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);
    }
}

Upvotes: 0

Related Questions