iOS开发-自动布局之autoresizingMask使用详解(Storyboard&Code)

简述

iOS有两大自动布局利器:autoresizing 和 autolayout(autolayout是iOS6以后新增)。autoresizing是UIView的属性,一直存在,使用也比较简单,但是没有autolayout那样强大。如果你的界面比较简单,那么你完全可以使用autoresizing去进行自动布局。

UIViewAutoresizing是一个枚举类型,默认是UIViewAutoresizingNone,也就是不做任何处理。

类型说明

UIViewAutoresizingNone:不会随父视图的改变而改变
UIViewAutoresizingFlexibleLeftMargin:自动调整view与父视图左边距,以保证右边距不变
UIViewAutoresizingFlexibleWidth:自动调整view的宽度,保证左边距和右边距不变
UIViewAutoresizingFlexibleRightMargin:自动调整view与父视图右边距,以保证左边距不变
UIViewAutoresizingFlexibleTopMargin:自动调整view与父视图上边距,以保证下边距不变
UIViewAutoresizingFlexibleHeight:自动调整view的高度,以保证上边距和下边距不变
UIViewAutoresizingFlexibleBottomMargin:自动调整view与父视图的下边距,以保证上边距不变

autoresizing组合使用

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

UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin

意思是:view的宽度按照父视图的宽度比例进行缩放,以保证左右边距不变;且动调整view与父视图的下边距,以保证上边距不变。

UIViewAutoresizingFlexibleLeftMargin  |UIViewAutoresizingFlexibleRightMargin

意思是:自动调整左右边距以保证view的宽度不变。 比如原来LeftMargin = 20,RightMargin = 30,调整后的距离应为LeftMargin = 68,RightMargin = 102,即68/20=102/30。

UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin 
| UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin

意思是:自动调整左右边距以保证view的宽度不变;且自动调整上下边距以保证view的高度不变。

注意

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

2)从Xcode6开始,Storyboard&Xib默认是自动布局,因此我们需要手动调整,才能使用autoresizing。

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

举例

- (void)viewDidLoad {
    [super viewDidLoad];

    NSInteger margin = 10;
    NSInteger width = [UIScreen mainScreen].bounds.size.width - margin * 2;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(margin, 64, width, 44)];
    [topView setBackgroundColor:[UIColor redColor]];
    
    NSInteger textLabelWidth = 200;
    NSInteger textLabelHeight = 30;
    CGFloat textLabelLeft = (topView.frame.size.width - textLabelWidth) / 2;
    CGFloat textLabelTop = (topView.frame.size.height - textLabelHeight) / 2;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(textLabelLeft, textLabelTop, 200, 30)];
    // 设置文字及居中
    [textLabel setText:@"Garvey"];
    [textLabel setTextAlignment:NSTextAlignmentCenter];
    [textLabel setTextColor:[UIColor whiteColor]];// 添加视图
    
    
    //UIViewAutoresizingFlexibleWidth:自动调整view的宽度,保证左边距和右边距不变
    textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    //UIViewAutoresizingFlexibleWidth:自动调整view的宽度,保证左边距和右边距不变
    //UIViewAutoresizingFlexibleBottomMargin:自动调整view与父视图的下边距,以保证上边距不变
    topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;    
    
    [topView addSubview:textLabel];
    [self.view addSubview:topView];

参考文章

http://www.cnblogs.com/GarveyCalvin/p/4165151.html

转载于:https://my.oschina.net/hejunbinlan/blog/912743

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值