iOS 7 status bar like iOS 6

前端 未结 5 1312
南笙
南笙 2021-02-08 04:05

I have an app with a support landscape and portrait mode. And I need the same behavior status bar like on iOS 6. What is the simplest way to do this?

I\'ve tried the sol

5条回答
  •  抹茶落季
    2021-02-08 05:04

    1) It's a hack, but it works!

    Use it if you doesn't use UIAlertView or KGStatusBar!!!

    #import 
    
    @interface UIScreen (I_love_ios_7)
    - (CGRect)bounds2;
    - (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation;
    @end
    
    @implementation UIScreen (I_love_ios_7)
    - (CGRect)bounds2
    {
        return [self boundsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
    }
    
    - (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation
    {
        CGRect resultFrame = [self bounds2];
        if(UIInterfaceOrientationIsLandscape(orientation))
            resultFrame.size.width -= 20;
        else
            resultFrame.size.height -= 20;
        return resultFrame;
    }
    @end
    
    void Swizzle(Class c, SEL orig, SEL new)
    {
        Method origMethod = class_getInstanceMethod(c, orig);
        Method newMethod = class_getInstanceMethod(c, new);
        if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
            class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
        else
            method_exchangeImplementations(origMethod, newMethod);
    }
    
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
            Swizzle([UIScreen class], @selector(bounds2), @selector(bounds));
            [application setStatusBarStyle:UIStatusBarStyleLightContent];
            self.window.clipsToBounds =YES;
    
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(applicationDidChangeStatusBarOrientation:)
                                                         name:UIApplicationWillChangeStatusBarOrientationNotification
                                                       object:nil];
            NSDictionary* userInfo = @{UIApplicationStatusBarOrientationUserInfoKey : @([[UIApplication sharedApplication] statusBarOrientation])};
            [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillChangeStatusBarOrientationNotification
                                                                object:nil
                                                              userInfo:userInfo];
        }
    
        return YES;
    }
    
    - (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
    {
        UIInterfaceOrientation orientation = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
        CGSize size = [[UIScreen mainScreen] boundsForOrientation:orientation].size;
        int w = size.width;
        int h = size.height;
        float statusHeight = 20.0;
        switch(orientation){
            case UIInterfaceOrientationPortrait:
                self.window.frame =  CGRectMake(0,statusHeight,w,h);
                break;
            case UIInterfaceOrientationPortraitUpsideDown:
                self.window.frame =  CGRectMake(0,0,w,h);
                break;
            case UIInterfaceOrientationLandscapeLeft:
                self.window.frame =  CGRectMake(statusHeight,0,w,h);
                break;
            case UIInterfaceOrientationLandscapeRight:
                self.window.frame =  CGRectMake(0,0,w,h);
                break;
        }
    }
    @end
    

    2) Create category, and always use contentView instead of view

    @interface UIViewController(iOS7_Fix)
    @property (nonatomic, readonly) UIView* contentView;
    - (void)updateViewIfIOS_7;
    @end
    
    @implementation UIViewController(iOS7_Fix)
    static char defaultHashKey;
    - (UIView *)contentView
    {
        return objc_getAssociatedObject(self, &defaultHashKey)?: self.view;
    }
    
    - (void)setContentView:(UIView *)val
    {
        objc_setAssociatedObject(self, &defaultHashKey, val, OBJC_ASSOCIATION_RETAIN_NONATOMIC) ;
    }
    
    - (void)updateViewIfIOS_7
    {
        if([[[UIDevice currentDevice] systemVersion] floatValue] < 7 || objc_getAssociatedObject(self, &defaultHashKey))
            return;
    
        UIView* exchangeView = [[UIView alloc] initWithFrame:self.view.bounds];
        exchangeView.autoresizingMask = self.view.autoresizingMask;
        exchangeView.backgroundColor = [UIColor blackColor];
    
        UIView* view = self.view;
        if(self.view.superview){
            [view.superview addSubview:exchangeView];
            [view removeFromSuperview];
        }
        [exchangeView addSubview:view];
        self.view = exchangeView;
    
        CGRect frame = self.view.bounds;
        frame.origin.y += 20.0;
        frame.size.height -= 20.0;
        view.frame = frame;
        view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
        [self setContentView:view];
    }
    

    In every UIViewController:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self updateViewIfIOS_7];
        UILabel* lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];
        lab.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:lab];
        //...
    }
    

    Portrait Landscape

提交回复
热议问题