iOS 7 status bar back to iOS 6 default style in iPhone app?

前端 未结 25 1167
终归单人心
终归单人心 2020-11-22 05:48

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

\"GUI

25条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 06:25

    Updates on 19th Sep 2013:

    fixed scaling bugs by adding self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);

    corrected typos in the NSNotificationCenter statement


    Updates on 12th Sep 2013:

    corrected UIViewControllerBasedStatusBarAppearance to NO

    added a solution for apps with screen rotation

    added an approach to change the background color of the status bar.


    There is, apparently, no way to revert the iOS7 status bar back to how it works in iOS6.

    However, we can always write some codes and turn the status bar into iOS6-like, and this is the shortest way I can come up with:

    1. 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 the UIApplicationstatusBarStyle method.)

    2. In AppDelegate's application:didFinishLaunchingWithOptions, call

      if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
          [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;
      


    in order to:

    1. Check if it's iOS 7.

    2. Set status bar's content to be white, as opposed to UIStatusBarStyleDefault.

    3. Avoid subviews whose frames extend beyond the visible bounds from showing up (for views animating into the main view from top).

    4. Create the illusion that the status bar takes up space like how it is in iOS 6 by shifting and resizing the app's window frame.


    For apps with screen rotation,

    use NSNotificationCenter to detect orientation changes by adding

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(applicationDidChangeStatusBarOrientation:)
    name:UIApplicationDidChangeStatusBarOrientationNotification
    object:nil];
    

    in if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) and create a new method in AppDelegate:

    - (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
    {
        int a = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
        int w = [[UIScreen mainScreen] bounds].size.width;
        int h = [[UIScreen mainScreen] bounds].size.height;
        switch(a){
            case 4:
                self.window.frame =  CGRectMake(0,20,w,h);
                break;
            case 3:
                self.window.frame =  CGRectMake(-20,0,w-20,h+20);
                break;
            case 2:
                self.window.frame =  CGRectMake(0,-20,w,h);
                break;
            case 1:
               self.window.frame =  CGRectMake(20,0,w-20,h+20);
        }
    }
    

    So that when orientation changes, it will trigger a switch statement to detect app's screen orientation (Portrait, Upside Down, Landscape Left, or Landscape Right) and change the app's window frame respectively to create the iOS 6 status bar illusion.


    To change the background color of your status bar:

    Add

     @property (retain, nonatomic) UIWindow *background;
    

    in AppDelegate.h to make background a property in your class and prevent ARC from deallocating it. (You don't have to do it if you are not using ARC.)

    After that you just need to create the UIWindow in if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1):

    background = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, self.window.frame.size.width, 20)];
    background.backgroundColor =[UIColor redColor];
    [background setHidden:NO];
    

    Don't forget to @synthesize background; after @implementation AppDelegate!

提交回复
热议问题