一、autoresizingMask
这是IOS旧的约束实现方式,而且只能用于约束父子关系的控件,约束子控件相对于父控件的位置关系。支持的约束种类有:
typedefNS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone =0,
UIViewAutoresizingFlexibleLeftMargin =1<<0,
UIViewAutoresizingFlexibleWidth =1<<1,
UIViewAutoresizingFlexibleRightMargin =1<<2,
UIViewAutoresizingFlexibleTopMargin =1<<3,
UIViewAutoresizingFlexibleHeight =1<<4,
UIViewAutoresizingFlexibleBottomMargin =1<<5
};
UIViewAutoresizingNone:不随父View的变化而变化
UIViewAutoresizingFlexibleLeftMargin:相对父布局左边距弹性变化,右边距固定
UIViewAutoresizingFlexibleRightMargin:相对父布局右边距弹性变化,左边距固定
UIViewAutoresizingFlexibleTopMargin:相对父布局上边距弹性变化,底部边距固定
UIViewAutoresizingFlexibleBottomMargin:相对父布局底部边距弹性变化,顶部边距固定
UIViewAutoresizingFlexibleWidth:相对父布局的宽度等比缩放
UIViewAutoresizingFlexibleHeight:想对父布局的高度等比缩放
注意:所有的属性都是位移运算,因此可以叠加
如:设置约束,距离左边弹性,距离上部弹性以及宽度弹性
self.view1.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
二、AutoLayout
AutoLayout是从autoresizingMask演化而来的,因此需要禁用autoresizingMask的方式。
translatesAutoresizingMaskIntoConstraints = NO;
AutoLayout的功能强大,可以设置任意View的约束关系。但添加约束有对应的规则,规则如下:
1、针对View的独立属性,不需要依赖其他View,则将约束添加到自己上面
//设置View的宽度
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:200];
//设置View的高度
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:200];
[self.view1addConstraint:widthConstraint];
[self.view1addConstraint:heightConstraint];
2、针对有依赖关系的情况,有以下规则:
(1)两个同层级view之间的约束关系,添加到他们共同的父view上
(2)两个不同层级上的view之间的约束关系,添加到他们最近的共同的父view上
(3)两个有层次关系的view之间的约束关系,添加到层次较高的view上
请谨记此规则,如果添加错了则会崩溃:(我将约束添加到了自己上面,没有添加到父View上)
2020-04-17 17:35:42.950274+0800 ConstraitLayoutTest[1216:429986] [LayoutConstraints] The view hierarchy is not prepared for the constraint:
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2020-04-17 17:35:42.951995+0800 ConstraitLayoutTest[1216:429986] [LayoutConstraints] View hierarchy unprepared for constraint.
Constraint:
Container hierarchy:
<UIView: 0x101407f90; frame = (0 0; 0 0); layer = <CALayer: 0x28344f560>>
View not found in container hierarchy: >
That view's superview: NO SUPERVIEW
2020-04-17 17:35:42.953478+0800 ConstraitLayoutTest[1216:429986] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint: view:>'
*** First throw call stack:
(0x237f97ea0 0x237169a40 0x237e9e674 0x23897bd60 0x2651378c8 0x265137d94 0x265138494 0x100d9a3f0 0x100d99bac 0x26479620c 0x26479663c 0x2646c91f8 0x2646bd4cc 0x2647a7b24 0x2647aaa38 0x2647ab09c 0x2647aa990 0x2647aaccc 0x100d989e8 0x264d5e768 0x2647ec6d0 0x2647ec9f0 0x2647eb9f0 0x264d9818c 0x264d993f0 0x264d786ec 0x264e4457c 0x264e46f74 0x264e3fa64 0x237f281cc 0x237f2814c 0x237f27a30 0x237f228fc 0x237f221cc 0x23a199584 0x264d5d054 0x100d98be8 0x2379e2bb4)
libc++abi.dylib: terminating with uncaught exception of type NSException
Message from debugger: The LLDB RPC server has crashed. The crash log is located in ~/Library/Logs/DiagnosticReports and has a prefix 'lldb-rpc-server'. Please file a bug and attach the most recent crash log.