iOS 适配

BaseViewController的纯计算版本

/**********   华丽分割线  ************/

更多的详情请登陆俊哥博客:http://my.oschina.net/u/1418722/blog

/**********   华丽分割线  ************/

俊哥语录:

这个类只要输入你的导航条实际高度,可以返回给你在各种适配情况下,导航条的frame和除导航条外的区域的frame
同样支持从IOS5到IOS7,隐藏和显示状态栏的情况,以及IPhone4和IPhone5的屏幕尺寸,以及支持IPhone和iPad两种设备
我写这个是为了在用XIB创建ViewController的情况下,用原来的BaseViewController不方便
BaseViewController在纯代码的开发过程中比较好用
我为了通用,加了- (void)setBarHeight:(CGFloat)barHeight;这个方法
用来设置你自定义的导航条高度,后面的计算都是基于这个高度的
另外两个方法看方法名字可以知道是干嘛的了
不用实例化,直接用两个类方法
后面跟你的导航条高度
注意,这个高度是不计算状态栏的
基本上可以通用任何情况
为了适配问题我死了不少脑细胞啊

/**********   华丽分割线  ************/

使用的例子:

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.navigationBarView.frame = [AdaptiveServer getBaseNavigationBarFrameWithNavigationBarHeight:44];
   
    self.detailBackgroundView.frame = [AdaptiveServer getBaseBackgroundViewFrameWithNavigationBarHeight:44];
   
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:tap];
   
   
}

/**********   华丽分割线  ************/

代码详情:

@interface AdaptiveServer : NSObject

@property (nonatomic, assign) CGRect navigationBarFrame;
@property (nonatomic, assign) CGRect backgroundViewFrame;

- (void)setBarHeight:(CGFloat)barHeight;

+ (CGRect)getBaseNavigationBarFrameWithNavigationBarHeight:(CGFloat)barHeight;
+ (CGRect)getBaseBackgroundViewFrameWithNavigationBarHeight:(CGFloat)barHeight;

@end



#import "AdaptiveServer.h"

@interface AdaptiveServer()

@property (nonatomic, assign) CGFloat navigationBarHeight;

@end

@implementation AdaptiveServer

- (id)init
{
    self = [super init];
    if (self) {
       
    }
   
    return self;
}

- (void)setBarHeight:(CGFloat)barHeight
{
    self.navigationBarHeight = barHeight;
}

- (void)setup
{
    if ([UIApplication sharedApplication].statusBarHidden == YES) {
       
        [self statusBarIsHidden];
       
    }else {
       
        [self statusBarIsShow];
    }
}

- (void)statusBarIsHidden
{
    [self autoLayoutWithV6Height:self.navigationBarHeight V7Height:self.navigationBarHeight];
}

- (void)statusBarIsShow
{
    [self autoLayoutWithV6Height:self.navigationBarHeight V7Height:self.navigationBarHeight + 20];
}

- (void)autoLayoutWithV6Height:(CGFloat)v6Height V7Height:(CGFloat)v7Height
{
    CGRect baseFrame = [UIScreen mainScreen].bounds;
    self.navigationBarFrame = baseFrame;
    self.backgroundViewFrame = baseFrame;
   
    CGRect tmpNavigationBarFrame = self.navigationBarFrame;
    CGRect tmpBackgroundViewFrame = self.backgroundViewFrame;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

        tmpNavigationBarFrame.size.height = v7Height;
        tmpBackgroundViewFrame.size.height = baseFrame.size.height - v7Height;
        tmpBackgroundViewFrame.origin.y = v7Height;
       
    }else {

        tmpNavigationBarFrame.size.height = v6Height;
        tmpBackgroundViewFrame.size.height = baseFrame.size.height - v6Height;
        tmpBackgroundViewFrame.origin.y = v6Height;
    }
   
    self.navigationBarFrame = tmpNavigationBarFrame;
    self.backgroundViewFrame = tmpBackgroundViewFrame;
}

+ (CGRect)getBaseNavigationBarFrameWithNavigationBarHeight:(CGFloat)barHeight
{
    AdaptiveServer *adaptiveServer = [[AdaptiveServer alloc] init];
    [adaptiveServer setBarHeight:barHeight];
    [adaptiveServer setup];
    return adaptiveServer.navigationBarFrame;
}

+ (CGRect)getBaseBackgroundViewFrameWithNavigationBarHeight:(CGFloat)barHeight
{
    AdaptiveServer *adaptiveServer = [[AdaptiveServer alloc] init];
    [adaptiveServer setBarHeight:barHeight];
    [adaptiveServer setup];
    return adaptiveServer.backgroundViewFrame;
}

@end

 

posted on 2013-12-23 21:12  songbai  阅读(483)  评论(0编辑  收藏  举报