经典屏幕适配,两句代码搞定
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;
- 以frame代码实现为例
@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 ,项目中用这个开发会比较快