autoresizingMask 是UIView的一个属性。这是个枚举,用来调整当前视图与父视图之间的位置关系,用于动态布局,使用时需要设置
self.autoresizesSubviews = YES;
枚举值如下。
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
};
例如下面代码块表示当前的imageView 和父视图self.view之间自动拉伸宽和高,左右距离和顶部底部的距离不变
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,100,300,150)];
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:imageView];
//<pre name="code" class="html">假设屏幕宽度为320时 imageView的frame为CGRectMake(10,100,100,300);当屏幕变为480,则frame的宽变为300*480/320,高度变为150*480/320