gpt4 book ai didi

详解iOS应用开发中autoresizing尺寸自动适应属性的用法

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章详解iOS应用开发中autoresizing尺寸自动适应属性的用法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

前言:现在已经不像以前那样只有一个尺寸,现在最少的iphone开发需要最少需要适配三个尺寸。因此以前我们可以使用硬坐标去设定各个控件的位置,但是现在的话已经不可以了,我们需要去做适配,也许你说可以使用两套ui或两套以上的ui,但那样不高效也不符合设计。ios有两大自动布局利器:autoresizing 和 autolayout(autolayout是ios6以后新增)。autoresizing是uiview的属性,一直存在,使用也比较简单,但是没有autolayout那样强大。如果你的界面比较简单,要求的细节没有那么高,那么你完全可以使用autoresizing去进行自动布局。以下会针对autoresizing进行讨论.

零、autoresizing使用前的解释:

uiviewautoresizing是一个枚举类型,默认是uiviewautoresizingnone,也就是不做任何处理.

复制代码 代码如下:

typedef ns_options(nsuinteger, uiviewautoresizing) {
    uiviewautoresizingnone                 = 0,
    uiviewautoresizingflexibleleftmargin   = 1 << 0,
    uiviewautoresizingflexiblewidth        = 1 << 1,
    uiviewautoresizingflexiblerightmargin  = 1 << 2,
    uiviewautoresizingflexibletopmargin    = 1 << 3,
    uiviewautoresizingflexibleheight       = 1 << 4,
    uiviewautoresizingflexiblebottommargin = 1 << 5
};

各属性解释:

  。

详解iOS应用开发中autoresizing尺寸自动适应属性的用法

在这里说明一下,如果是经常使用storyboard/xib设置autoresizing,那么转变使用代码设置autoresizing的话,容易出现理解错误问题。比如说uiviewautoresizingflexibletopmargin,也许会被误认为是顶部距离不变,其实是底部距离不变。这个解决办法也很简单,只需要把使用代码和使用storyboard设置autoresizing,它们是相反的,只需要这样去记就可以了.

autoresizing组合使用:

也就是枚举中的值可以使用|隔开,同时拥有多个值的功能,可以针对不同的场景作不同的变化。例如:

uiviewautoresizingflexiblewidth | uiviewautoresizingflexiblebottommargin 意思是:view的宽度按照父视图的宽度比例进行缩放,距离父视图顶部距离不变.

其它的组合类似,我这里就不一一列举了.

注意:

1)view的autoresizessubviews属性为yes时(默认为yes),autoresizing才会生效.

2)从xcode6开始,storyboard&xib默认是自动布局,因此我们需要手动调整,才能使用autoresizing.

具体操作如图(打开storyboard文件,你就会看到下面图的界面):

详解iOS应用开发中autoresizing尺寸自动适应属性的用法

下面会写一个简单的例子以给予你们更直观的理解,并会在本文最后附上demo下载地址,请继续往下观看噢.

demo:

1)顶部距离父视图距离不变 。

2)宽度按父视图比例进行拉伸 。

3)view与父视图的左边距和右边距不变 。

详解iOS应用开发中autoresizing尺寸自动适应属性的用法

1、使用代码(code)控制autoresizingmask 。

下面是项目用到的宏:

复制代码 代码如下:

#define topspace 64
#define kmargin 20
 
#define ktopviewheight 44
#define ktopviewwidth 300
 
#define ktextlabelwidth 200
#define ktextlabelheight 30

没有做适配之前的代码:

复制代码 代码如下:

// 以iphone4(320, 480)为基础,设置各控件的位置
// 注意:必须所有控件都按照iphone4(320, 480)为基础初始化一次,不然按比例缩放时会发生错误!
uiview *topview = [[uiview alloc] initwithframe:cgrectmake(kmargin, topspace, ktopviewwidth, ktopviewheight)];
cgfloat textlabeltop = (topview.frame.size.width - ktextlabelwidth) / 2;
cgfloat textlabelwidth = (topview.frame.size.height - ktextlabelheight) / 2;
uilabel *textlabel = [[uilabel alloc] initwithframe:cgrectmake(textlabeltop, textlabelwidth, ktextlabelwidth, ktextlabelheight)];
 
// 设置文字及居中
[textlabel settext:@"garvey"];
[textlabel settextalignment:nstextalignmentcenter];
 
// 分别设置样式
[topview setbackgroundcolor:[uicolor redcolor]];
[textlabel settextcolor:[uicolor whitecolor]];// 添加视图
[topview addsubview:textlabel];
[self.view addsubview:topview];

它将会显示:

  。

详解iOS应用开发中autoresizing尺寸自动适应属性的用法

使用autoresizing进行界面适配:

补充:你可以先按其它的设备尺寸为界面上的控件初始化,因为autoresizing是会以父视图的改变而改变.

复制代码 代码如下:

// 以iphone4(320, 480)为基础,设置各控件的位置
// 注意:必须所有控件都按照iphone4(320, 480)为基础初始化一次,不然按比例缩放时会发生错误!
uiview *topview = [[uiview alloc] initwithframe:cgrectmake(kmargin, ktopspace, ktopviewwidth, ktopviewheight)];
cgfloat textlabeltop = (topview.frame.size.width - ktextlabelwidth) / 2;
cgfloat textlabelwidth = (topview.frame.size.height - ktextlabelheight) / 2;
uilabel *textlabel = [[uilabel alloc] initwithframe:cgrectmake(textlabeltop, textlabelwidth, ktextlabelwidth, ktextlabelheight)];
 
// 设置文字及居中
[textlabel settext:@"garvey"];
[textlabel settextalignment:nstextalignmentcenter];
 
// 分别设置样式
[topview setbackgroundcolor:[uicolor redcolor]];
[textlabel settextcolor:[uicolor whitecolor]];
 
// 设置文字控件的宽度按照上一级视图(topview)的比例进行缩放
[textlabel setautoresizingmask:uiviewautoresizingflexiblewidth];
// 设置view控件的宽度按照父视图的比例进行缩放,距离父视图顶部、左边距和右边距的距离不变
[topview setautoresizingmask:uiviewautoresizingflexiblewidth | uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexiblerightmargin | uiviewautoresizingflexibleleftmargin];
 
// 添加视图
[topview addsubview:textlabel];
[self.view addsubview:topview];
 
// 注意:重新设置topview位置的代码,必须要写在添加视图的后面,不然autoresizing的位置计算会出错!
cgfloat topviewwidth = kuiscreen.size.width - kmargin * 2;
[topview setframe:cgrectmake(kmargin, ktopspace, topviewwidth, ktopviewheight)];

最后显示:

  。

详解iOS应用开发中autoresizing尺寸自动适应属性的用法

2、在storyboard控制autoresizingmask 。

autoresizingmask无论是在storyboard中实现还是用代码实现,本质上来说就是六条线.

在storyboard中使用autoresizingmask需要将autolayout禁用掉.

如图:

详解iOS应用开发中autoresizing尺寸自动适应属性的用法

  。

图中共有六条线(上下左右到外围的距离线和内部的两条线).

如果上下左右的线中的某一条选中,则表示距离父控件的边界的距离不变.

比如图中选中左和上的线,表示蓝色view距离self.view的左边界和上边界的距离不变.

如果同时选中上下左右的线,那么行为会和选中上左的线是一样的,也就是默认的行为.

如果想让子控件尺寸跟随父控件尺寸改变而改变,那么需要用到内部的两条线。内部的线表示允许控件在水平和垂直方向上可拉伸。如果不选中内部的线,说明是不希望控件可拉伸的,也就是固定大小的.

也就是说,周围的线选中表示固定距离,内部的线选中表示允许拉伸.

如果用代码设置autoresizingmask,会发现一个view的autoresizingmask属性是一个枚举:

复制代码 代码如下:

uiviewautoresizingnone                 = 0,
uiviewautoresizingflexibleleftmargin   = 1 << 0,
uiviewautoresizingflexiblewidth        = 1 << 1,
uiviewautoresizingflexiblerightmargin  = 1 << 2,
uiviewautoresizingflexibletopmargin    = 1 << 3,
uiviewautoresizingflexibleheight       = 1 << 4,
uiviewautoresizingflexiblebottommargin = 1 << 5

除了第一个是none,剩下的六个就对应storyboard中的六条线.

  。

注意上面 。

复制代码 代码如下:

uiviewautoresizingflexiblewidth
uiviewautoresizingflexibleheight

这是正好和storyboard中设置autoresizingmask中内部的两天线是一致的:可拉伸的宽度和高度.

  。

剩下的需要注意一下:

复制代码 代码如下:

uiviewautoresizingflexibleleftmargin
uiviewautoresizingflexiblerightmargin 
uiviewautoresizingflexibletopmargin   
uiviewautoresizingflexiblebottommargin

在storyboard中,如果选中周围的线,表示距离固定。比如我选中了左边的线,那么表示我想要左边的距离固定,所以在代码中我应该选择uiviewautoresizingflexiblerightmargin,即表示右边的距离不固定,那么则默认左边的距离固定.

  。

autoresizingmask的缺点是只能保证父控件和子控件间的关系,无法保证同级控件间的关系,也就是说有些需求,autoresizingmask是无法实现的。更多的精力应该放在autolayout上.

最后此篇关于详解iOS应用开发中autoresizing尺寸自动适应属性的用法的文章就讲到这里了,如果你想了解更多关于详解iOS应用开发中autoresizing尺寸自动适应属性的用法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com