苹果在iOS2
中引入了Autoresizing
技术用于屏幕适配, 其用于指定当父视图的bounds
发生改变时如何自动调整子视图的布局布局
Code
使用Autoresizing
技术Autoresizing
技术涉及到两个UIView
的属性: autoresizesSubviews
属性用于标识当自身的bounds
发生改变时是否自动调整子视图的布局; autoresizingMask
属性用于标识当父视图的bounds
发生改变时如何自动调整自身的布局ui
// 默认为YES
@property(nonatomic) BOOL autoresizesSubviews;
// 默认为UIViewAutoresizingNone
@property(nonatomic) UIViewAutoresizing autoresizingMask;复制代码
autoresizingMask
属性的取值为UIViewAutoresizing
枚举, 能够采用位运算(按位或)同时设置多个值atom
UIViewAutoresizingNone = 0, 不执行任何调整
UIViewAutoresizingFlexibleLeftMargin = 1 << 0, 自动调整与父视图的左边距
UIViewAutoresizingFlexibleWidth = 1 << 1, 自动调整自身的宽度
UIViewAutoresizingFlexibleRightMargin = 1 << 2, 自动调整与父视图的右边距
UIViewAutoresizingFlexibleTopMargin = 1 << 3, 自动调整与父视图的上边距
UIViewAutoresizingFlexibleHeight = 1 << 4, 自动调整自身的高度
UIViewAutoresizingFlexibleBottomMargin = 1 << 5, 自动调整与父视图的下边距复制代码
注: 每一个值仅用于标识子视图如何调整指定维度的布局. 例如:
UIViewAutoresizingFlexibleLeftMargin
仅用于标识子视图自动调整与父视图的左边距, 并不影响如何调整自身的宽度和与父视图的右边距spa
若是UIView
的autoresizesSubviews
属性设置为YES
, 当它的bounds
发生改变时, 它会根据每一个子视图的autoresizingMask
属性设置自动为其调整布局3d
// 示例: 让子视图的宽度和高度保持不变, 且与父视图的右边距和下边距均固定为20pt
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(width-120, height-120, 100, 100)];
redView.backgroundColor = [UIColor redColor];
redView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:redView];复制代码
若是UIView
的autoresizingMask
属性在同一个轴线上同时设置了多个值, 默认行为是根据各自维度的可调整部分按照比例来分配调整部分. 例如: 若是UIView
的autoresizingMask
属性设置在x
轴上包含UIViewAutoresizingFlexibleWidth
和UIViewAutoresizingFlexibleRightMargin
, 而不包含UIViewAutoresizingFlexibleLeftMargin
, 则表明与父视图的左边距是固定的, 而自身的宽度和与父视图右边距会根据各自维度的可调整部分按照比例发生变化code
// 示例: 让子视图与父视图的右边距和下边距均固定为20pt, 且与父视图的左边距和自身的宽度按照比例自动调整
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(width-120, height-120, 100, 100)];
redView.backgroundColor = [UIColor redColor];
redView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:redView];复制代码
IB
使用Autoresizing
技术IB
中的设置最终都会转化为Code
运行, 因此咱们在这部分只须要了解如何设置autoresizesSubviews
属性和autoresizingMask
属性便可cdn
咱们在父视图的Attributes inspector
中设置autoresizesSubviews
属性, 用于标识当自身的bounds
发生改变时是否自动调整子视图的布局blog
咱们在子视图的中设置autoresizingMask
属性, 用于标识当父视图的bounds
发生改变时如何自动调整自身的布局it
autoresizingMask
属性的设置由六根线标识, 其中位于外部的四根线分别用于标识如何自动调整与父视图的上、下、左、右边距; 位于内部的两根线分别用于标识如何自动调整自身的宽度和高度io
注: 位于外部的四根线(柱状线段), 若是是实线即表明不自动调整; 位于内部的两根线(箭头线段), 若是是实线即表明自动调整
下面再次经过IB
的方式实现Code
方式的两个示例
示例: 让子视图的宽度和高度保持不变, 且与父视图的右边距和下边距均固定为20pt
示例: 让子视图与父视图的右边距和下边距均固定为20pt, 且与父视图的左边距和自身的宽度按照比例自动调整
注: 若是
Xcode
中默认开启了Autolayout
技术, 在使用Autoresizing
技术前须要手动将其关闭
IB
中的设置最终都会转化为Code
运行, 因此两者在本质上没有任何区别. 可是在设置autoresizingMask
属性时, 两者的思考方式不一样
在Code
中, autoresizingMask
属性显式设置的值即表明自动调整, 未设置的值即表明不自动调整
在IB
中, autoresizingMask
属性位于外部的四根线(柱状线段), 若是是实线即表明不自动调整; 位于内部的两根线(箭头线段), 若是是实线即表明自动调整
下面经过两个示例对比说明
示例: 让子视图与父视图的上、下、左、右边距保持不变, 且自身的宽度和高度自动调整
redView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;复制代码
示例: 让子视图与父视图的上、下、左、右边距以及自身的宽度和高度按照比例自动调整
redView.autoresizingMask = UIViewContentModeLeft | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;复制代码
注: 若是
Autoresizing
技术不能知足你对界面的精确布局需求, 你能够使用一个自定义视图做为容器, 并经过重写其layoutSubviews
方法来精确地布局其子视图, 或者使用iOS6
新引入的Autolayout
技术来进行精确布局