纯代码约束使用AoutLayout

在对控件进行约定时使用纯代码可使用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。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值