iOS开发资料收集区

经典屏幕适配,两句代码搞定

2016-04-27  本文已影响266人  pzliOS

屏幕适配

#define kScreen_height  [[UIScreen mainScreen] bounds].size.height
#define kScreen_width   [[UIScreen mainScreen] bounds].size.width
//  4/4s 5/5s 320  6/6s 375  6p/6sp 414   美工给的基本图,比如基准图是6,则设置如下
static const CGFloat baseScreenWidth = 320.0f;
//  4/4s 修改480 5/5s 568  6/6s 667  6p/6sp 736
static const CGFloat baseScreenHeight = 568.0f;
static CGFloat scaleXAndWidth;
static CGFloat scaleYAndHeight;
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    
    scaleXAndWidth = kScreen_width/baseScreenWidth;
    scaleYAndHeight = kScreen_height/baseScreenHeight;
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(12*scaleXAndWidth, 88*scaleYAndHeight, 296*scaleXAndWidth, 35*scaleYAndHeight)];
    
    btn.backgroundColor = [UIColor redColor];
    [self.view addSubview:btn];
    
}
@end  

如果配合使用Masonry进行适配会快很多 ,Masonry后期再补......因为也挺简单的,只是需要区别mas_equalTo和equalTo ,项目中用这个开发会比较快

上一篇下一篇

猜你喜欢

热点阅读