IOS6之AutoLayout(二)

IOS6之AutoLayout(一)

http://blog.csdn.net/zfpp25_/article/details/8861221

IOS6之AutoLayout(二)

http://blog.csdn.net/zfpp25_/article/details/8861855

IOS6之AutoLayout(三)

http://blog.csdn.net/zfpp25_/article/details/8861958

IOS6之AutoLayout(四)

http://blog.csdn.net/zfpp25_/article/details/8862040

IOS6之AutoLayout(五)

http://blog.csdn.net/zfpp25_/article/details/8862157



继IOS6之AutoLayout(一)简单讲解了的”上沿“、”下沿“、”左沿“、”右沿“相对布局方法之后,本篇讲解相对布局中的居中。


直接看实现代码:


UIButton *secondButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
 [secondButtonsetTitle:@"Second"forState:UIControlStateNormal];
 [secondButton sizeToFit];
 secondButton.translatesAutoresizingMaskIntoConstraints =NO;
[self.viewaddSubview:secondButton];

NSLayoutConstraint *constraint;


//secondButton的中心点的X坐标与self.view的中心点X坐标距离为零,也就是中心点X坐标重合

   

 constraint = [NSLayoutConstraint
                  constraintWithItem:secondButton
                  attribute:NSLayoutAttributeCenterX
                  relatedBy:NSLayoutRelationEqual
                  toItem:self.view
                  attribute:NSLayoutAttributeCenterX
                  multiplier:1.0f
                  constant:0.0f];
    
    [self.view addConstraint:constraint];

    //secondButton的中心点的X坐标与self.view的中心点Y坐标距离为零,也就是中心点Y坐标重合

constraint = [NSLayoutConstraint
                  constraintWithItem:secondButton
                  attribute:NSLayoutAttributeCenterY
                  relatedBy:NSLayoutRelationEqual
                  toItem:self.view
                  attribute:NSLayoutAttributeCenterY
                  multiplier:1.0f
                  constant:0.0f];

    

//  指定按钮的宽度为200.0f

    

constraint = [NSLayoutConstraint
                  constraintWithItem:secondButton
                  attribute:NSLayoutAttributeWidth
                  relatedBy:NSLayoutRelationEqual
                  toItem: nil
                  attribute:NSLayoutAttributeNotAnAttribute
                  multiplier:1.0f
                  constant:200.0f];
   
    [self.view addConstraint:constraint];

    

//  指定按钮的高度为60.0f

    

constraint = [NSLayoutConstraint
                  constraintWithItem:secondButton
                  attribute:NSLayoutAttributeHeight
                  relatedBy:NSLayoutRelationEqual
                  toItem: nil
                  attribute:NSLayoutAttributeNotAnAttribute
                  multiplier:1.0f
                  constant:60.0f];
    
    [self.view addConstraint:constraint];


以上代码中,指定按钮中心点的相对左边容易理解,但指定宽度和高度的时候,NSLayoutAttributeNotAnAttribute如何理解 ?

下面是官网的解释:

Constraints are of the form "view1.attr1 <relation> view2.attr2 * multiplier + constant". If the constraint you wish to express does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.


其实也不难理解了。如果有不理解内容可参考IOS6之AutoLayout(一)




转载请保留,原文链接:http://blog.csdn.net/lizhongfu2013/article/details/8861855

若发现有不合适或错误之处,还请批评指正,不胜感激。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zfpp25_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值