屏幕適配
- 根據不同的屏幕尺寸,按照等比例縮放生成的對象
#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 ,項目中用這個開發會比較快