ios 屏幕适配

来源:互联网 发布:java中排序算法 编辑:程序博客网 时间:2024/06/15 05:50

相对于其他操作系统来讲,ios的适配是极其简单的,ios的项目是十分精致的,ios的屏幕适配相关代码:

if (UIScreenWidth == 320)    {        if (UIScreenHeight == 480)        {//            NSLog(@"4   320*480");        }else if(UIScreenHeight == 568)        {//            NSLog(@"5   320*568");        }        _memberSc.frame = CGRectMake(175, 5, 95, 35);    }else if(UIScreenWidth == 375)    {//        NSLog(@"6   375*667");        _memberSc.frame = CGRectMake(190, 5, 140, 40);    }else if(UIScreenWidth == 414)    {//        NSLog(@"6p  414*736");        _memberSc.frame = CGRectMake(195, 5, 150, 40);    }

其实autolayout或者是xib适配并不是很方便,需要你有一定的功底,其中著名的第三方库有:
***Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性而且同时支持 iOS 和 Max OS X
代码适配如下*
无论是用storyBoard还是代码,在设置控件之间layout关系的时候,我们都需要设置控件的位置属性。在下面的方法中,这个位置属性就是NSLayoutAttribute对象,他决定的控件对象的参照位置:
这里写图片描述

例如我们创建一个label,将其尺寸设置为50*50,放在屏幕中间,使用如下代码:

**添加约束**UILabel * label = [[UILabel alloc]init];    [self.view addSubview:label];    [label mas_makeConstraints:^(MASConstraintMaker *make) {        make.center.equalTo(self.view);        make.height.equalTo(@50);        make.width.equalTo(@50);    }];    label.backgroundColor = [UIColor redColor];

当我们需要配合布局改变或者动画效果的时候,我们可能需要将已经添加的约束进行更新操作,使用如下的方法:

**更新约束**[label mas_updateConstraints:^(MASConstraintMaker *make) {        make.height.equalTo(@100);        make.width.equalTo(@100);    }];

更新约束的作用在于更新已经添加的某些约束,并不会移除掉原有的约束,如果我们需要添加新的约束,可以使用下面的重设约束的方法

**重设约束**[label mas_remakeConstraints:^(MASConstraintMaker *make) {        make.left.equalTo(self.view.mas_left).offset(10);        make.top.equalTo(self.view.mas_top).offset(100);        make.height.equalTo(@100);        make.width.equalTo(@100);    }];

在添加具体约束的时候,我们不仅可以将约束值设置为绝对的相等关系,也可以设置一些值域的关系,在Masonry中,有如下三种:

//绝对相等- (MASConstraint * (^)(id attr))equalTo;//大于等于- (MASConstraint * (^)(id attr))greaterThanOrEqualTo;//小于等于- (MASConstraint * (^)(id attr))lessThanOrEqualTo;

如果不想用第三方的,那么就自己手撸代码:::::

//屏幕适配        if (UIScreenWidth == 320)        {            if (UIScreenHeight == 480)            {                NSLog(@"4   320*480");                _bgview.frame = CGRectMake(0, 5, 170, 35);                liveIcon.frame = CGRectMake(0, 0, 35, 35);                nameLabel.frame = CGRectMake(35, 0, 70, 17.5);                audienceNum.frame = CGRectMake(35, 17.5, 70,17.5);                favorBt.frame = CGRectMake(105, 5, 60, 25);                _shutDownButton.frame = CGRectMake(275, 8, 35,35);            }else if(UIScreenHeight == 568)            {                NSLog(@"5   320*568");                _bgview.frame = CGRectMake(0, 5, 170, 35);                liveIcon.frame = CGRectMake(0, 0, 35, 35);                nameLabel.frame = CGRectMake(35, 0, 70, 17.5);                audienceNum.frame = CGRectMake(35, 17.5, 70,17.5);                favorBt.frame = CGRectMake(105, 5, 60, 25);                _shutDownButton.frame = CGRectMake(275, 8, 35,35);            }            _bgview.layer.cornerRadius = 17.5f;        }else if(UIScreenWidth == 375)        {            NSLog(@"6   375*667");            _bgview.frame = CGRectMake(0, 5, 185, 40);            liveIcon.frame = CGRectMake(0, 0, 40, 40);            nameLabel.frame = CGRectMake(40, 0, 70, 20);            audienceNum.frame = CGRectMake(40, 20, 70, 20);            favorBt.frame = CGRectMake(110, 5, 70, 30);            _shutDownButton.frame = CGRectMake(330, 8, 40,40);        }else if(UIScreenWidth == 414)        {            NSLog(@"6p  414*736");            _bgview.frame = CGRectMake(0, 5, 185, 40);            liveIcon.frame = CGRectMake(0, 0, 40, 40);            nameLabel.frame = CGRectMake(40, 0, 70, 20);            audienceNum.frame = CGRectMake(40, 20, 70, 20);            favorBt.frame = CGRectMake(110, 5, 70, 30);            _shutDownButton.frame = CGRectMake(360, 8, 40,40);        }    }
0 0