經典屏幕適配,兩句代碼搞定

屏幕適配

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容