iOS 7 status bar translucency with backwards compatibility

空扰寡人 提交于 2019-12-03 03:43:43

If you are using auto layout, then all you need to do is add a Vertical Constraint from your top most view to Top Layout Guide as shown below and it should take care the top spacing.

For more info: https://developer.apple.com/library/ios/qa/qa1797/_index.html

You can use the new Xcode 5 feature of iOS6/7 deltas to set -20 to all your view, which will give you a similar experience. Set your views correctly for iOS7 in interface builder, and use deltas for iOS6 support.

Here's what I did to always pad the top of my view with 20px (height of the status bar).

I used this code in my AppDelegate's application:didFinishLaunchingWithOptions: method

...
// container holds my root view controller
UINavigationController *container = [UINavigationController alloc] init...];
...

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // iOS 7
    // Create parent controller that will contain your existing root view controller's view shifted 20 px down to account for the status bar.
    UIViewController *newRootController = [[UIViewController alloc] init];

    // Add my old root view controller as a child
    [newRootController addChildViewController:container];

    // Add its view as a subview
    [newRootController.view addSubview:container.view];

    // Call this method because it does some configuration?
    [container didMoveToParentViewController:newRootController];

    // Now just position the view starting at point (0, 20)
    UIView *aView = container.view;

    NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(aView);
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[aView]|" options:0 metrics:nil views:viewDictionary];

    [newRootController.view addConstraints:constraints];
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aView]|" options:0 metrics:nil views:viewDictionary];

    [newRootController.view addConstraints:constraints];

    self.window.rootViewController = newRootController;
} else { // pre iOS 7
    self.window.rootViewController = container;
}

Now whenever you're in iOS 7, everything will exist in the root view controller's view that is shifted 20 pixels down. You'll only have to do this once in your AppDelegate.

Set UIViewControllerBasedStatusBarAppearance' to NO in info.plist (To opt out of having view controllers adjust the status bar style so that we can set the status bar style by using theUIApplicationstatusBarStyle` method.)

In AppDelegate's application:didFinishLaunchingWithOptions, call

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-20);

    //Added on 19th Sep 2013
    self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
}
return YES;

You can disable the view going under the top bar in ios 7 by setting the following:

if([controller canPerformAction:@selector(setEdgesForExtendedLayout:) withSender:self]) {
        [controller setEdgesForExtendedLayout:UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!