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

被折叠的 条评论
为什么被折叠?



