美文网首页
iOS屏幕适配的两种方法

iOS屏幕适配的两种方法

作者: yymyb | 来源:发表于2016-04-06 20:08 被阅读2801次

第一种

添加两个文件  UIViewExt.h与UIViewExt.m文件

在ViewController.h文件中导入头文件

#import"UIViewExt.h"

然后宏定义 获取设备的高与宽

#define HEIGHT self.view.height

#define WIDTH self.view.width

然后在ViewController.m文件中初始化各种控件时就可以使用视图的相对位置

self.lblName=[[UILabelalloc]initWithFrame:CGRectMake(self.view.left+50,self.view.top+100,WIDTH/8,HEIGHT/16)];

self.lblPassworw=[[UILabelalloc]initWithFrame:CGRectMake(self.view.left+50,self.lblName.bottom+10,WIDTH/8,HEIGHT/16)];

第二种

这种方法是 等比缩放

首先在AppDelegate.h文件里面

宏定义 获取设备的高与宽

#define SCREENHEIGHT [[UIScreen mainScreen] bounds].size.height

#define SCREENWIDTH [[UIScreen mainScreen] bounds].size.width

接着声明两个属性变量

@property(assign,nonatomic)floatautoSizeScaleX;

@property(assign,nonatomic)floatautoSizeScaleY;

在AppDelegate.m文件里面

//初始化AppDelegate单例的方法

AppDelegate*myDelegate=[[UIApplicationsharedApplication]delegate];

//判断屏幕的高大于480即为iPhone5或以上设备因为它们屏幕都是等比增长的

if(SCREENHEIGHT>480)

{

/**

*以iPhone5为基准若是iPhone5

则myDelegate.autoSizeScaleX=SCREENWIDTH/320;

即为myDelegate.autoSizeScaleX=320/320;

若是iPhone6

则myDelegate.autoSizeScaleX=SCREENWIDTH/320;

即为myDelegate.autoSizeScaleX=375/320;

*/

myDelegate.autoSizeScaleX=SCREENWIDTH/320;

myDelegate.autoSizeScaleY=SCREENHEIGHT/568;

}

else{

/**

*否则即为iPhone4

*/

myDelegate.autoSizeScaleX=1.0;

myDelegate.autoSizeScaleY=1.0;

}

在ViewController.h文件中使用时导入头文件

#import"AppDelegate.h"

模仿系统的CGRectMake方法 重写一个CGRectMake1方法  在初始化控件时用这个方法就可以实现等比缩放 来失陪不同屏幕尺寸的iPhone

/**

*  CG_INLINE为内联函数

将CGRectMake重新定义为CGRectMake1

*

*  @param x      <#x description#>

*  @param y      <#y description#>

*  @param width  <#width description#>

*  @param height <#height description#>

*

*  @return rect的大小

*/

CG_INLINECGRect

CGRectMake1(CGFloatx,CGFloaty,CGFloatwidth,CGFloatheight)

{

CGRectrect;

AppDelegate*myDelegate=[[UIApplicationsharedApplication]delegate];

rect.origin.x= x *  myDelegate.autoSizeScaleX;

rect.origin.y= y *  myDelegate.autoSizeScaleY;

rect.size.width= width * myDelegate.autoSizeScaleX;

rect.size.height= height * myDelegate.autoSizeScaleY;

returnrect;

}

相关文章

网友评论

      本文标题:iOS屏幕适配的两种方法

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