美文网首页iOS开发资料收集区
经典屏幕适配,两句代码搞定

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

作者: pzliOS | 来源:发表于2016-04-27 14:08 被阅读266次

    屏幕适配

    • 根据不同的屏幕尺寸,按照等比例缩放生成的对象
    #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 ,项目中用这个开发会比较快

    相关文章

      网友评论

        本文标题:经典屏幕适配,两句代码搞定

        本文链接:https://www.haomeiwen.com/subject/wzecrttx.html