IOS6之AutoLayout(三)

来源:互联网 发布:不交钱的网络兼职 编辑:程序博客网 时间:2024/05/22 04:30

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

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