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的相对布局实现:

直接上代码:


@interface ViewController1 ()
{
    float aspect;
}
@property (nonatomic, strong) UIImageView *imageView;
@end

- (void)viewDidLoad
{
    [superviewDidLoad];
    self.view.backgroundColor = [UIColorwhiteColor];
    
    UIImage *image = [UIImageimageNamed:@"beautiful_girl.png"];
    
    self.imageView = [[UIImageViewalloc]initWithImage:image];
    self.imageView.translatesAutoresizingMaskIntoConstraints =NO;
    [self.viewaddSubview:self.imageView];
    
    aspect =self.imageView.frame.size.height/self.imageView.frame.size.width;
    
    [selfautoLayoutImage];
}

- (void) autoLayoutImage
{
//图片的中心点X坐标与self.view的中心点X左边相等
    NSLayoutConstraint *constraint = [NSLayoutConstraint
                                      constraintWithItem:self.imageView
                                      attribute:NSLayoutAttributeCenterX
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view
                                      attribute:NSLayoutAttributeCenterX
                                      multiplier:1.0f
                                      constant:0.0f];
    
    [self.view addConstraint:constraint];
    
//图片的中心点Y坐标与self.view的中心点Y左边相等
    constraint = [NSLayoutConstraint
                  constraintWithItem:self.imageView
                  attribute:NSLayoutAttributeCenterY
                  relatedBy:NSLayoutRelationEqual
                  toItem:self.view
                  attribute:NSLayoutAttributeCenterY
                  multiplier:1.0f
                  constant:0.0f];
    
[self.viewaddConstraint:constraint];
    
//图片的高度和self.view的高度相等
    constraint = [NSLayoutConstraint
                  constraintWithItem:self.imageView
                  attribute:NSLayoutAttributeHeight
                  relatedBy:NSLayoutRelationEqual
                  toItem:self.view
                  attribute:NSLayoutAttributeHeight
                  multiplier:1.0f
                  constant:0.0f];
    
[self.viewaddConstraint:constraint];
    
    
//     为了保持当屏幕旋转时候,图片的纵横比不变,如果不设置比例,图片比例会改变(multiplier:aspect)
    constraint = [NSLayoutConstraint
                  constraintWithItem:self.imageView
                  attribute:NSLayoutAttributeHeight
                  relatedBy:NSLayoutRelationEqual
                  toItem:self.imageView
                  attribute:NSLayoutAttributeWidth
                  multiplier:aspect
                  constant:0.0f];
    
[self.imageViewaddConstraint:constraint];
}


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


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

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zfpp25_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值