iOS开发——屏幕尺寸适配

对于屏幕尺寸适配,目前先指竖屏的方式适合方式1和2。

1.控件尺寸写死的方式,偶尔会用到屏幕的宽度和高度。

UILabel *holdLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 42, 100, 20 )];

[[UIButton alloc] initWithFrame:CGRectMake(self.width - 65, 0, 80, 80)];

其中  self.width     = [UIScreen mainScreen].bounds.size.width;

2.是在第一种的基础上乘上屏幕的宽高比

self.beginDate        = [[UILabel alloc]initWithFrame:CGRectMake(12* ScreenWidthRate, 145* ScreenHeightRate,width/2 -12* ScreenWidthRate, 15* ScreenHeightRate)];

其中,ScreenWidthRate和ScreenHeightRate  是在pch文件里定义好的,如下: (比例根据UI图)

#define ScreenHeightRate ([[UIScreen mainScreen] bounds].size.height / 667.0)  //6 plus 736  6s/6 667  5s 568
#define ScreenWidthRate ([UIScreen mainScreen].bounds.size.width / 375.0)

static inline float screenHeightRate() {
    
    CGFloat width = [UIScreen mainScreen].bounds.size.height;
    
    return width / 667.0;
}

static inline float screenWidthRate() {
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    
    return width / 375.0;
}

#endif

3.第三种的屏幕适配是采用目前流行的Masonry 自动布局

这个推荐使用,并单独一篇博客介绍。

适合复杂cell里的控件布局和横竖屏的适配。

原文地址:https://www.cnblogs.com/LiuChengLi/p/5160717.html