IOS6之AutoLayout(二)

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


直接看实现代码:


  1. UIButton *secondButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];  
  2.  [secondButtonsetTitle:@"Second"forState:UIControlStateNormal];  
  3.  [secondButton sizeToFit];  
  4.  secondButton.translatesAutoresizingMaskIntoConstraints =NO;  
  5. [self.viewaddSubview:secondButton];  
  6.   
  7. NSLayoutConstraint *constraint;  


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

   

  1. constraint = [NSLayoutConstraint  
  2.                  constraintWithItem:secondButton  
  3.                  attribute:NSLayoutAttributeCenterX  
  4.                  relatedBy:NSLayoutRelationEqual  
  5.                  toItem:self.view  
  6.                  attribute:NSLayoutAttributeCenterX  
  7.                  multiplier:1.0f  
  8.                  constant:0.0f];  
  9.      
  10.    [self.view addConstraint:constraint];  

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

  1. constraint = [NSLayoutConstraint  
  2.                   constraintWithItem:secondButton  
  3.                   attribute:NSLayoutAttributeCenterY  
  4.                   relatedBy:NSLayoutRelationEqual  
  5.                   toItem:self.view  
  6.                   attribute:NSLayoutAttributeCenterY  
  7.                   multiplier:1.0f  
  8.                   constant:0.0f];  

    

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

    

  1. constraint = [NSLayoutConstraint  
  2.                   constraintWithItem:secondButton  
  3.                   attribute:NSLayoutAttributeWidth  
  4.                   relatedBy:NSLayoutRelationEqual  
  5.                   toItem: nil  
  6.                   attribute:NSLayoutAttributeNotAnAttribute  
  7.                   multiplier:1.0f  
  8.                   constant:200.0f];  
  9.      
  10.     [self.view addConstraint:constraint];  

    

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

    

  1. constraint = [NSLayoutConstraint  
  2.                   constraintWithItem:secondButton  
  3.                   attribute:NSLayoutAttributeHeight  
  4.                   relatedBy:NSLayoutRelationEqual  
  5.                   toItem: nil  
  6.                   attribute:NSLayoutAttributeNotAnAttribute  
  7.                   multiplier:1.0f  
  8.                   constant:60.0f];  
  9.       
  10.     [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://write.blog.csdn.net/postedit/8861855

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值