自动布局之autoresizing使用详解(Storyboard&code)

之前一直看到在Xcode边上有个autoresizing,不过一直没怎么用到过 今天得空正好百度了下 原来和autolayout一样是用来布局的,下面是一个哥们对于这个的理解,个人觉得很不错,分享给大家看看.

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
};

各属性解释:
 
UIViewAutoresizingNone
 
不会随父视图的改变而改变
UIViewAutoresizingFlexibleLeftMargin
 
自动调整view与父视图左边距,以保证右边距不变
UIViewAutoresizingFlexibleWidth
 
自动调整view的宽度,保证左边距和右边距不变
UIViewAutoresizingFlexibleRightMargin
 
自动调整view与父视图右边距,以保证左边距不变
UIViewAutoresizingFlexibleTopMargin
 
自动调整view与父视图上边距,以保证下边距不变
UIViewAutoresizingFlexibleHeight
 
自动调整view的高度,以保证上边距和下边距不变
UIViewAutoresizingFlexibleBottomMargin
 
自动调整view与父视图的下边距,以保证上边距不变
 
 
 
在这里说明一下,如果是经常使用Storyboard/Xib设置autoresizing,那么转变使用代码设置autoresizing的话,容易出现理解错误问题。比如说UIViewAutoresizingFlexibleTopMargin,也许会被误认为是顶部距离不变,其实是底部距离不变。这个解决办法也很简单,只需要把使用代码和使用Storyboard设置autoresizing,它们是相反的,只需要这样去记就可以了。


autoresizing组合使用:(这里是比较重要的一点)
 
也就是枚举中的值可以使用|隔开,同时拥有多个值的功能,可以针对不同的场景作不同的变化。例如:
 
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin
意思是:view的宽度按照父视图的宽度比例进行缩放,距离父视图顶部距离不变。
 
其它的组合类似,我这里就不一一列举了。
 
 
 
注意:view的autoresizesSubviews属性为yes时(默认为yes),autoresizing才会生效。


使用autoresizing进行界面适配:

补充:你可以先按其它的设备尺寸为界面上的控件初始化,因为autoresizing是会以父视图的改变而改变。
下面是项目用到的宏:
#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];

使用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)];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值