iOS屏幕适配的几种方式

在iOS开发中,会经常碰到屏幕适配的问题,下面来说一下自己在项目开发中遇到的几种屏幕适配的方式.

第一种:

利用宽高比,在不同的屏幕中来进行对控件的位置与控件的宽高进行等比例缩放.选定一个型号的屏幕的宽高为基准,进行等比例缩放.例如以iPhone6或者iPhone6s为基准.

其宽高分别是375与667.Iphone6ScaleWidth =  [UIScreen mainScreen].bounds.size.width/375; Iphone6ScaleHeight = [UIScreen mainScreen].bounds.size.height/667;

如果是iPhone6或者iPhone6s则Iphone6ScaleWidthIphone6ScaleHeight的值都为1.但是此方法对于iPhone5之后的型号可以适用.因为我们知道iPhone4或者iPhone4s尺寸是320x480.iPhone5是320x568.所以对于4或者4s来说计算得到的宽高比不相同.可能会造成一个方形控件,在iPhone5之后的机型上显示的都是正方形.但是在4或者4s上显示的是长方形.需要进行自己的判断(目前来说4或者4s市场上已经不多见了).

同时也可以在代理增加两个属性,宽高比例属性,例如:

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property float autoSizeScaleX;
@property float autoSizeScaleY;


在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if(CurrentScreenHeight > 480){
        nowAppDelegate().autoSizeScaleX = CurrentScreenWidth/320.0;
        nowAppDelegate().autoSizeScaleY = CurrentScreenHeight/568.0;
    }else{
        nowAppDelegate().autoSizeScaleX = 1.0;其中nowAppDelegate() = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        nowAppDelegate().autoSizeScaleY = 1.0;
    }

}

在项目对于控件的位置和尺寸乘以这两个缩放比例.

第二种:

使用autoLayout进行屏幕适配.代码如下:

    UIView *headView = [[UIView alloc] init];
    headView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:headView];
    headView.backgroundColor = [UIColor redColor];
    NSLayoutConstraint *headViewLeft = [NSLayoutConstraint constraintWithItem:headView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    NSLayoutConstraint *headViewTop = [NSLayoutConstraint constraintWithItem:headView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    NSLayoutConstraint *headViewRight = [NSLayoutConstraint constraintWithItem:headView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    NSLayoutConstraint *headViewHeight = [NSLayoutConstraint constraintWithItem:headView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:200];
    self.headHeightCons = headViewHeight;
    [self.view addConstraints:@[headViewTop,headViewLeft,headViewRight]];
    [headView addConstraint:headViewHeight];
    

    UIImage *imageLol = [UIImage imageNamed:@"lol"];
    UIImageView *imageViewLol = [[UIImageView alloc] initWithImage:imageLol];
    [headView addSubview:imageViewLol];
    imageViewLol.translatesAutoresizingMaskIntoConstraints = NO;
    [imageViewLol setContentMode:UIViewContentModeScaleAspectFill];

    
    NSLayoutConstraint *imageLolViewLeft = [NSLayoutConstraint constraintWithItem:imageViewLol attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:headView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    NSLayoutConstraint *imageViewLolViewTop = [NSLayoutConstraint constraintWithItem:imageViewLol attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:headView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    NSLayoutConstraint *imageViewLolViewRight = [NSLayoutConstraint constraintWithItem:imageViewLol attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:headView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    NSLayoutConstraint *imageViewLolViewBottom = [NSLayoutConstraint constraintWithItem:imageViewLol attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:headView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [headView addConstraints:@[imageLolViewLeft,imageViewLolViewTop,imageViewLolViewRight,imageViewLolViewBottom]];

第三种:使用框架Masonry进行屏幕适配.读者可以自己找到框架或者demo看下,在这里就不多说了.

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值