iOS 7状态栏,如iOS 6

Gra*_*lex 11 objective-c ios ios7

我有一个支持横向和纵向模式的应用程序.我需要像在iOS 6上一样的行为状态栏.最简单的方法是什么?

我已经尝试过Stack Overflow问题iOS 7状态栏中的解决方案回到iOS 6风格?,但它不起作用.我的子视图取决于视图大小,我的视图无法正确拉伸.我不想更新我的XIB文件; 我只想添加一些有助于我的东西.我不知道它是什么(黑客或祈祷).

Gra*_*lex -1

1)这是一个黑客,但它有效!

如果您不使用 UIAlertView 或 KGStatusBar,请使用它!

#import <objc/runtime.h>

@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
Run Code Online (Sandbox Code Playgroud)

2)创建类别,并始终使用contentView而不是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];
}
Run Code Online (Sandbox Code Playgroud)

在每一个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];
    //...
}
Run Code Online (Sandbox Code Playgroud)

肖像 景观