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



继续介绍Image的相对布局实现:

直接上代码:


  1. @interface ViewController1 ()  
  2. {  
  3.     float aspect;  
  4. }  
  5. @property (nonatomic, strong) UIImageView *imageView;  
  6. @end  
  7.   
  8. - (void)viewDidLoad  
  9. {  
  10.     [superviewDidLoad];  
  11.     self.view.backgroundColor = [UIColorwhiteColor];  
  12.       
  13.     UIImage *image = [UIImageimageNamed:@"beautiful_girl.png"];  
  14.       
  15.     self.imageView = [[UIImageViewalloc]initWithImage:image];  
  16.     self.imageView.translatesAutoresizingMaskIntoConstraints =NO;  
  17.     [self.viewaddSubview:self.imageView];  
  18.       
  19.     aspect =self.imageView.frame.size.height/self.imageView.frame.size.width;  
  20.       
  21.     [selfautoLayoutImage];  
  22. }  
  23.   
  24. - (void) autoLayoutImage  
  25. {  
  26. //图片的中心点X坐标与self.view的中心点X左边相等  
  27.     NSLayoutConstraint *constraint = [NSLayoutConstraint  
  28.                                       constraintWithItem:self.imageView  
  29.                                       attribute:NSLayoutAttributeCenterX  
  30.                                       relatedBy:NSLayoutRelationEqual  
  31.                                       toItem:self.view  
  32.                                       attribute:NSLayoutAttributeCenterX  
  33.                                       multiplier:1.0f  
  34.                                       constant:0.0f];  
  35.       
  36.     [self.view addConstraint:constraint];  
  37.       
  38. //图片的中心点Y坐标与self.view的中心点Y左边相等  
  39.     constraint = [NSLayoutConstraint  
  40.                   constraintWithItem:self.imageView  
  41.                   attribute:NSLayoutAttributeCenterY  
  42.                   relatedBy:NSLayoutRelationEqual  
  43.                   toItem:self.view  
  44.                   attribute:NSLayoutAttributeCenterY  
  45.                   multiplier:1.0f  
  46.                   constant:0.0f];  
  47.       
  48. [self.viewaddConstraint:constraint];  
  49.       
  50. //图片的高度和self.view的高度相等  
  51.     constraint = [NSLayoutConstraint  
  52.                   constraintWithItem:self.imageView  
  53.                   attribute:NSLayoutAttributeHeight  
  54.                   relatedBy:NSLayoutRelationEqual  
  55.                   toItem:self.view  
  56.                   attribute:NSLayoutAttributeHeight  
  57.                   multiplier:1.0f  
  58.                   constant:0.0f];  
  59.       
  60. [self.viewaddConstraint:constraint];  
  61.       
  62.       
  63. //     为了保持当屏幕旋转时候,图片的纵横比不变,如果不设置比例,图片比例会改变(multiplier:aspect)  
  64.     constraint = [NSLayoutConstraint  
  65.                   constraintWithItem:self.imageView  
  66.                   attribute:NSLayoutAttributeHeight  
  67.                   relatedBy:NSLayoutRelationEqual  
  68.                   toItem:self.imageView  
  69.                   attribute:NSLayoutAttributeWidth  
  70.                   multiplier:aspect  
  71.                   constant:0.0f];  
  72.       
  73. [self.imageViewaddConstraint:constraint];  
  74. }  


如果有地方不理解,可以参考IOS6之AutoLayout(一)、IOS6之AutoLayout(二)。


转载请保留,原文链接:http://write.blog.csdn.net/postedit/8861958

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值