autolayout自动布局代码敲写

本文详细介绍了如何使用自动布局(Autolayout)在iOS应用中适配图片与标签,通过代码实例展示了如何设置约束来实现图片左右与底层视图之间的固定间距、顶部与底层视图顶部相等,以及两个标签自适应高度的布局效果。

自从苹果出了6之后,适配成了一个重要的问题。幸好苹果提供了autolayout让我们使用,在这里我来说一下autolayout用代码敲的情况。(关于xib适配,大家自己实地的用一下应该都可以理解)

我直接上代码,我一直认为实践是最能出结果的一种做法。

这是我需要的效果:


首先我先把最顶上的图片适配:

#import "ViewController.h"


@interface ViewController ()

@property(nonatomic,weak)UIImageView *imageView;//图片

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor=[UIColor grayColor];

    //图片的加载

    [self loadImage];

    //加载中间label

    [self loadLabel1];

}


-(void)loadImage

{

    UIImageView *imageView=[[UIImageView alloc]init];

    imageView.image=[UIImage imageNamed:@"1.jpg"];

    [self.view addSubview:imageView];

    self.imageView=imageView;

    

    //自动布局

    [self.imageView setTranslatesAutoresizingMaskIntoConstraints:NO];

    //left

    NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:40.0f];

    [self.view addConstraint:con1];

    //right

    NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-40.0f];

    

    [self.view addConstraint:con2];

    //top

    NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeTop relatedBy: NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];

    [self.view addConstraint:con3];

    

    //height

    NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5f constant:0.0f];

    [self.view addConstraint:con4];

}

做到这,图片的适配已经做好了。就是左右与底层的仕途==视图都是40的间距,顶部与底层视图顶部相等。

然后把底下的两个label适配

-(void)loadLabel1

{

    UILabel *label=[[UILabel alloc]init];

    label.text=@"现在我为大家朗诵一首诗,这首诗的名字叫做现在我为大家朗诵一首诗,这首诗的名字叫做。。。。。”";

    label.numberOfLines=0;

    label.backgroundColor=[UIColor redColor];

    [self.view addSubview:label];

    

    //自动布局

    [label setTranslatesAutoresizingMaskIntoConstraints:NO];

    //left

    NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:0.0f];

    [self.view addConstraint:con1];

    

    //right

    NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:0.0f];

    [self.view addConstraint:con2];

    

    //top

    NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];

    [self.view addConstraint:con3];

    

    

    //第二个label

    UILabel *label2 = [[UILabel alloc]init];

    label2.text=@"锄禾日当午,旱地里下午。谁知盘中餐,粒粒皆辛苦。锄禾日当午,旱地里下午。谁知盘中餐,粒粒皆辛苦。锄禾日当午,旱地里下午。谁知盘中餐,粒粒皆辛苦。锄禾日当午,旱地里下午。谁知盘中餐,粒粒皆辛苦。锄禾日当午";

    label2.backgroundColor=[UIColor purpleColor];

    label2.numberOfLines=0;

    [self.view addSubview:label2];

    

    //自动布局

    [label2 setTranslatesAutoresizingMaskIntoConstraints:NO];

    

    //left

    NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeLeft multiplier:1.0f constant:0.0f];

    //[self.view addConstraint:con4];

    

    //right

    NSLayoutConstraint *con5 = [NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeRight multiplier:1.0f constant:0.0f];

    //[self.view addConstraint:con5];

    

    //top

    NSLayoutConstraint *con6 = [NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];

    //[self.view addConstraint:con6];

    

//    NSLayoutConstraint *con7 = [NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];

    

    con4.active=YES;

    con5.active=YES;

    con6.active=YES;

}


这里需要说一下,两个label我都没有给他高度,是让他自适应高度的。你也可以给他高度,但是如果内容不够的话,他会 把内容居中显示。
这是一个完成的适配代码,可以直接运行,你可以试试。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值