在对控件进行约定时使用纯代码可使用AoutLayout对控件进行约束,这样方便后期对控件的修改和适配不同屏幕尺寸大小。
一,要对控件是用aoutlayout进行约束,就要关闭控件的约束:
“控件”.translatesAutoresizingMaskIntoConstraints = ON;
二,对控件的顶部,底部,宽,高,左,右,中心
[self.view addConstraint: [NSLayoutConstraint constraintWithItem:“控件”attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:<span style="font-family: Menlo;">"控件的偏移位置"</span>]];//顶部
<pre name="code" class="objc" style="font-size: 13px;"> [self.view addConstraint: [NSLayoutConstraint constraintWithItem:<span style="font-family: Menlo;">“控件”</span><span style="font-family: Menlo;">attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:</span><span style="font-family: Menlo;">"控件的偏移位置"</span><span style="font-family: Menlo;">]];//底部</span>
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:“控件” attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1 constant:
"控件的偏移位置"]];//宽 [self.view addConstraint: [NSLayoutConstraint constraintWithItem:“控件” attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:
"控件的偏移位置"
]];//高
<span style="font-family:Menlo;"></span><pre name="code" class="objc" style="font-size: 13px;"><pre name="code" class="objc"> [self.view addConstraint: [NSLayoutConstraint constraintWithItem:<span style="font-family: Menlo;">“控件”</span><span style="font-family: Menlo;"> attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:</span><span style="font-family: Menlo;">"控件的偏移位置"]];//左</span>
[self.view addConstraint: [NSLayoutConstraint constraintWithItem:<span style="font-family: Menlo;">“控件”</span>attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:<span style="font-family: Menlo;">"控件的偏移位置"]];//右</span>
[self.view addConstraint: [NSLayoutConstraint constraintWithItem:“控件” attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:
"控件的偏移位置"
]];//中心
三,注意事项:
1,控件偏移值位置,根据你的需求进行一个设置正负的(CGFlout)偏移值
2,在对控件的宽高约束时,要注意toItme的填写,在对宽高的约束是,可以不参照其他控件进行约束,所以设置为空nil。