iOS 7 status bar back to iOS 6 default style in iP

2018-12-31 07:18发布

In iOS 7 the UIStatusBar has been designed in a way that it merges with the view like this:

GUI designed by Tina Tavčar (GUI designed by Tina Tavčar)

  • It is cool, but it will somewhat mess up your view when you have something at the top part of your view, and it becomes overlapped with the status bar.

  • Is there a simple solution (such as setting a property in info.plist) that can change the way it works [not overlapping] back to how it is in iOS6?

  • I know a more straightforward solution is to have self.view.center.x + 20 points for every single view controller, but changing them will screw other dimensions up (having a different self.view.center.x can cause problem to custom segues, etc.) and suddenly it turns into a tedious job that is best to be avoided.

  • I'll really be glad if someone can provide me an one-liner solution for this.

P.S. I know I can hide the status bar by doing things like having

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

In didFinishLaunchingWithOptions method, but that's a workaround, a shortcut avoiding the problem, so I don't consider that a real solution.

25条回答
零度萤火
2楼-- · 2018-12-31 07:42

Apple released Technical Q&A QA1797: Preventing the Status Bar from Covering Your Views. It works fine for the iOS 6 and iOS 7 versions.

查看更多
呛了眼睛熬了心
3楼-- · 2018-12-31 07:42

Try this simple method....

Step 1:To change in single viewController

[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];

Step 2: To change in whole application

info.plist
      ----> Status Bar Style
                  --->UIStatusBarStyle to UIStatusBarStyleBlackOpaque

Step 3: Also add this in each viewWillAppear to adjust statusbar height for iOS7

    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7) {
        CGRect frame = [UIScreen mainScreen].bounds;
        frame.origin.y+=20.0;
        frame.size.height-= 20.0;
        self.view.frame = frame;
        [self.view layoutIfNeeded];
    }
查看更多
春风洒进眼中
4楼-- · 2018-12-31 07:42

I have achieved status bar like iOS 6 in iOS 7.

Set UIViewControllerBasedStatusBarAppearance to NO in info.plist

Pase this code in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    self.window.clipsToBounds =YES;
    self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height);

    //Added on 19th Sep 2013
    NSLog(@"%f",self.window.frame.size.height);
    self.window.bounds = CGRectMake(0,0, self.window.frame.size.width, self.window.frame.size.height);
}

It may push down all your views by 20 pixels.To over come that use following code in -(void)viewDidAppear:(BOOL)animated method

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    CGRect frame=self.view.frame;
    if (frame.size.height==[[NSUserDefaults standardUserDefaults] floatForKey:@"windowHeight"])
    {
        frame.size.height-=20;
    }
    self.view.frame=frame;
}

You have to set windowHeight Userdefaults value after window allocation in didFinishLauncing Method like

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[NSUserDefaults standardUserDefaults] setFloat:self.window.frame.size.height forKey:@"windowHeight"];
查看更多
心情的温度
5楼-- · 2018-12-31 07:42

You can hide the status bar all together. So your app will be full-screen. I think that's the best you will get.

UIStatusBarStyleNone or set in the target settings.

查看更多
永恒的永恒
6楼-- · 2018-12-31 07:43

There is an option in the Interface Builder which calls the iOS 6/7 Delta property which aim to solve the offset problem.

Take a look at it in Stack Overflow question Interface Builder: What are the UIView's Layout iOS 6/7 Deltas for?.

查看更多
还给你的自由
7楼-- · 2018-12-31 07:46

In order to continue working with setStatusBarHidden: I use this category:

@interface UIApplication (StatusBar)

-(void)setIOS7StatusBarHidden:(BOOL)statusBarHidden;

@end

@implementation UIApplication (StatusBar)

-(void)setIOS7StatusBarHidden:(BOOL)statusBarHidden{
    if (!IOS7) {
        [self setStatusBarHidden:statusBarHidden];
        return;
     }

    if ([self isStatusBarHidden] == statusBarHidden) {
        return;
    }

    [self setStatusBarHidden:statusBarHidden];
    [self keyWindow].clipsToBounds = YES;
    CGFloat offset = statusBarHidden ? 0 : 20;
    [self keyWindow].frame =  CGRectMake(0,offset,[self keyWindow].frame.size.width,[self keyWindow].frame.size.height-offset);
    [self keyWindow].bounds = CGRectMake(0, offset, [self keyWindow].frame.size.width,[self keyWindow].frame.size.height);
}

@end
查看更多
登录 后发表回答