前段时间时间总是在纠结这个ios的Autolayout的布局问题。总感觉Apple这样做很麻烦,感觉Autolayout的代码写起来很别扭。半个眼都不想去看那些代码,又臭又长。
其实不然,当现在Apple发布的各个产品上市之后,设备的屏幕分辨率也有了很大的变化,iPhone4/4S是3.5寸,iPhone5是4.0寸,ipad2/new ipad/ipad4是9.7寸的,ipad mini是7.9的,分辨率也各不相同,
要是当我们需要开发一款产品要同时在这些设备上完美运行的时候,如果不用Autolayout,光靠一个View根本就是行不通的,当然,你也可以为一个ViewController做出几个不同的尺寸的View来,但是这样会大大的影响到开发的速度;鄙人以前就那么干过。一个Controller控制2个View分别适配3.5寸和4寸的屏幕。
但是如果用的Autolayout之后,根本就不用那么麻烦。更大化的加快了开发效率。
好了,下来闲话少说,上点实际的东西。
一般运用Autolayout的方法可以直接在IB里面用,用法大概如下:
首先点击当前View,勾选Use Autolayout选项,表示已经启用了AutoLayout
选择某一个Control然后在属性窗口选择如下:
点击齿轮形状的按钮选择Select and Edit
Relation选项分别表示大于/等于/小于
Constant表示当前控件相对于SuperView的x/y的相对距离
Priority表示优先级;关于优先级,Apple官方代码是这么说的:
enum {
UILayoutPriorityRequired = 1000, // a required constraint. Do not exceed this.
UILayoutPriorityDefaultHigh = 750, // this is the priority level with which a button resists compressing its content.
UILayoutPriorityDefaultLow = 250, // this is the priority level at which a button hugs its contents horizontally.
UILayoutPriorityFittingSizeLevel = 50, // When you send -[UIView systemLayoutSizeFittingSize:], the size fitting most closely to the target size (the argument) is computed. UILayoutPriorityFittingSizeLevel is the priority level with which the view wants to conform to the target size in that computation. It's quite low. It is generally not appropriate to make a constraint at exactly this priority. You want to be higher or lower.
};
typedef float UILayoutPriority;
也可以两个控件进行相对定位,以确保控件在各个尺寸的屏幕下面还保持原有的距离值;
上面说的都是用IB直接操作,下面说下关于代码如何操作
直接上一段代码,看注释:
- (void)viewDidLoad
{
UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setTitle:@"Click Me" forState:UIControlStateNormal];
[btn1 setTranslatesAutoresizingMaskIntoConstraints:NO];//标记是否自动布局
[self.view addSubview:btn1];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn2 setTitle:@"Click Me Please" forState:UIControlStateNormal];
[btn2 setTranslatesAutoresizingMaskIntoConstraints:NO];//标记是否自动布局
[self.view addSubview:btn2];
NSDictionary *views=NSDictionaryOfVariableBindings(btn1,btn2);
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(==50)-[btn1(100)]"
options:0
metrics:nil
views:views]];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(==50)-[btn1(30)]"
options:0
metrics:nil
views:views]];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:[btn2(==150)]" //H=Horizontal,水平方向,同时设定控件宽度
options:0
metrics:nil
views:views]];
[self.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=200)-[btn2(==btn1)]" //V=Vertical,垂直方向,同时设定控件高度
options:0
metrics:nil
views:views] ];
//注意AddConstraints和AddConstraint之间的区别,一个添加的参数是(NSArray *),一个是(NSLayoutConstraint *)
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:btn2
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:btn1
attribute:NSLayoutAttributeRight
multiplier:1
constant:10]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:btn2
attribute:NSLayoutAttributeTop //要设定的属性
relatedBy:NSLayoutRelationGreaterThanOrEqual //大于还是小于相对的View的值
toItem:btn1 //相对于某个View或者控件
attribute:NSLayoutAttributeTop //指定要设定的关联View的属性
multiplier:1 //因子值
constant:0]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
上面代码中
constraintsWithVisualFormat:参数为NSString型,指定Contsraint的属性,是垂直方向的限定还是水平方向的限定,参数定义一般如下:
V:|-(>=XXX) :表示垂直方向上相对于SuperView大于、等于、小于某个距离
若是要定义水平方向,则将V:改成H:即可
在接着后面-[]中括号里面对当前的View/控件 的高度/宽度进行设定;
options:字典类型的值;这里的值一般在系统定义的一个enum里面选取
typedef NS_OPTIONS(NSUInteger, NSLayoutFormatOptions) {
NSLayoutFormatAlignAllLeft = (1 << NSLayoutAttributeLeft),
NSLayoutFormatAlignAllRight = (1 << NSLayoutAttributeRight),
NSLayoutFormatAlignAllTop = (1 << NSLayoutAttributeTop),
NSLayoutFormatAlignAllBottom = (1 << NSLayoutAttributeBottom),
NSLayoutFormatAlignAllLeading = (1 << NSLayoutAttributeLeading),
NSLayoutFormatAlignAllTrailing = (1 << NSLayoutAttributeTrailing),
NSLayoutFormatAlignAllCenterX = (1 << NSLayoutAttributeCenterX),
NSLayoutFormatAlignAllCenterY = (1 << NSLayoutAttributeCenterY),
NSLayoutFormatAlignAllBaseline = (1 << NSLayoutAttributeBaseline),
NSLayoutFormatAlignmentMask = 0xFFFF,
/* choose only one of these three
*/
NSLayoutFormatDirectionLeadingToTrailing = 0 << 16, // default
NSLayoutFormatDirectionLeftToRight = 1 << 16,
NSLayoutFormatDirectionRightToLeft = 2 << 16,
NSLayoutFormatDirectionMask = 0x3 << 16,}
metrics:nil;一般为nil ,参数类型为NSDictionary,从外部传入 //衡量标准
views:就是上面所加入到NSDictionary中的绑定的View
在这里要注意的是 AddConstraints 和 AddConstraint 之间的区别,一个添加的参数是NSArray,一个是NSLayoutConstraint
下面看看Apple官方对这几个Constraint的说明:
/* Create constraints explicitly. Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant"
If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
*/
+(id)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
/* Create an array of constraints using an ASCII art-like visual format string.
*/
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;
这只是本人自己总结出来的一点拙见,望网友指正;
微信公众平台已开通,加个关注呗。我们一起学习,一起进步
微信号:ios开发总汇
百度知道群:开发者俱乐部
//H=Horizontal,水平方向,同时设定控件宽度