iOS开发 全机型适配解决方法
最近做项目,对于IPhone手机机型适配很是头疼,所以整理下网上资料,记录下来,也许能帮助到正看文章的你,
今天打算跟大家聊聊最近研究的全机型适配思路。
当前我们需要适配的iPhone机型有4s、5s、6s、6Plus四种机型。它们的尺寸分别是
iphone4s{320,480} 960*640
iphone55s{320,568} 1136*640
iphone66s {375,667} 1334*750
iphone6Plus6sPlus{414,736} 1920*1080
而一般我习惯在实际的项目开发中,使用Masonary来搭建UI界面,虽然在Masonary中我们能很方便的设置各个控件之间的约束,但是对于类似4s机型和6sPlus机型的很大的高度差,有时候仅仅靠一次性成型的约束还是搭建不出很合理的界面。
于是在这次搭建UI的过程中,我的一个思路就是按照比例,针对各个机型进行微调。思路如下:
美工提供的效果图是基于iPhone6的效果图
而我只需要将标注上的每个尺寸去对比iPhone6换算出比例,这样一些间距就能按照不同机型尺寸的比例变得不一样。
针对考虑交互体验的控件,在保持尺寸不变的基础上,做细节微调。
在具体的代码中,我封装出了一个类,定义了两个类方法专门去适配所有机型的高度和宽度。思路就是上述按不同机型针对于iPhone6的比例而适配。
代码我也贴一部分出来。
头文件的定义
#import<Foundation/Foundation.h>
#import<UIKit/UIKit.h>
typedefNS_ENUM(NSInteger,IPhoneType){
iPhone4Type=0,
iPhone5Type,
iPhone6Type,
iPhone6PlusType
};
@interfaceCalculateLayout:NSObject
/**
*基于UI设计的iPhone6设计图的全机型高度适配
*
*@paramheightView高度
*
*@return适配后的高度
*/
+(CGFloat)neu_layoutForAlliPhoneHeight:(CGFloat)height;
/**
*基于UI设计的iPhone6设计图的全机型宽度适配
*
*@paramwidth宽度
*
*@return适配后的宽度
*/
+(CGFloat)neu_layoutForAlliPhoneWidth:(CGFloat)width;
.m文件的部分如下:
#defineiPhone4Height(480.f)
#defineiPhone4Width(320.f)
#defineiPhone5Height(568.f)
#defineiPhone5Width(320.f)
#defineiPhone6Height(667.f)
#defineiPhone6Width(375.f)
#defineiPhone6PlusHeight(736.f)
#defineiPhone6PlusWidth(414.f)
#pragmamark-适配所有机型高度
+(CGFloat)neu_layoutForAlliPhoneHeight:(CGFloat)height{
CGFloatlayoutHeight=0.0f;
if(iPhone4){
layoutHeight=(height/iPhone6Height)*iPhone4Height;
}elseif(iPhone5){
layoutHeight=(height/iPhone6Height)*iPhone5Height;
}elseif(iPhone6){
layoutHeight=(height/iPhone6Height)*iPhone6Height;
}elseif(iPhone6P){
layoutHeight=(height/iPhone6Height)*iPhone6PlusHeight;
}else{
layoutHeight=height;
}
returnlayoutHeight;
}
+(CGFloat)neu_layoutForAlliPhoneWidth:(CGFloat)width{
CGFloatlayoutWidth=0.0f;
if(iPhone4){
layoutWidth=(width/iPhone6Width)*iPhone4Width;
}elseif(iPhone5){
layoutWidth=(width/iPhone6Width)*iPhone5Width;
}elseif(iPhone6){
layoutWidth=(width/iPhone6Width)*iPhone6Width;
}elseif(iPhone6P){
layoutWidth=(width/iPhone6Width)*iPhone6PlusWidth;
}
returnlayoutWidth;
}
代码我也已经放在了Github上,如果这些对你有帮助,在clone代码之余能否给个star。
感谢阅读,希望能帮助到大家,谢谢大家对本站点的支持!