IOS6之AutoLayout(二)

来源:互联网 发布:站外优化的理解 编辑:程序博客网 时间:2024/06/08 16:34

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(一)简单讲解了的”上沿“、”下沿“、”左沿“、”右沿“相对布局方法之后,本篇讲解相对布局中的居中。


直接看实现代码:


[cpp] view plaincopyprint?
  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坐标重合

   

[cpp] view plaincopyprint?
  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坐标重合

[cpp] view plaincopyprint?
  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

    

[cpp] view plaincopyprint?
  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

    

[cpp] view plaincopyprint?
  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
原创粉丝点击