masonry怎样适配scrollview
1个回答
2015-11-29
展开全部
– (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView * view1 = [[UIView alloc]init];
[self.view addSubview:view1];
int padding = 20;
view1.frame = CGRectMake(padding, padding, (self.view.frame.size.width – 3 * padding) * 0.5, (self.view.frame.size.height – 3 * padding) * 0.5);
view1.backgroundColor = [UIColor greenColor];
UIView * view2 = [[UIView alloc]init];
[self.view addSubview:view2];
view2.frame = CGRectMake(CGRectGetMaxX(view1.frame) + 20, view1.frame.origin.y, view1.frame.size.width, view1.frame.size.height);
view2.backgroundColor = [UIColor redColor];
UIView * view3 = [[UIView alloc]init];
[self.view addSubview:view3];
view3.frame = CGRectMake(view1.frame.origin.x, CGRectGetMaxY(view1.frame) + 20, self.view.frame.size.width – 2 * padding, view1.frame.size.height);
view3.backgroundColor = [UIColor yellowColor];
}
代码写完,运行效果如下:
1
这里我在6与5s上都有运行过,都得到了我们想要的结果.当然是竖屏情况下,如果你横屏,就会发现变成这样,
2
其实不难理解,我们的代码已经尽可能的写的使其不用过多适配,当是从某方面说,这些frame还是死的数值,所以竖屏他就没有去适配.那么,如果我们使用masonry来编写代码呢…来试试看,代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int padding = 20;
UIView * view1 = [[UIView alloc]init];
[self.view addSubview:view1];
view1.backgroundColor = [UIColor greenColor];
UIView * view2 = www.hbbz08.com [[UIView alloc]init];
[self.view addSubview:view2];
view2.backgroundColor = [UIColor redColor];
UIView * view3 = [[UIView alloc]init];
[self.view addSubview:view3];
view3.backgroundColor = [UIColor yellowColor];
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(padding);
make.left.equalTo(self.view).with.offset(padding);
make.right.equalTo(view2.left).with.offset(-padding);
make.bottom.equalTo(view3.top).with.offset(-padding);
make.width.equalTo(view2);
make.height.equalTo(view3);
}];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(padding);
make.right.equalTo(self.view).with.offset(-padding);
make.width.equalTo(view1);
make.height.equalTo(view3);
}];
[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).with.offset(padding);
make.right.equalTo(self.view).with.offset(-padding);
make.bottom.equalTo(self.view).with.offset(-padding);
make.height.equalTo(view1);
}];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView * view1 = [[UIView alloc]init];
[self.view addSubview:view1];
int padding = 20;
view1.frame = CGRectMake(padding, padding, (self.view.frame.size.width – 3 * padding) * 0.5, (self.view.frame.size.height – 3 * padding) * 0.5);
view1.backgroundColor = [UIColor greenColor];
UIView * view2 = [[UIView alloc]init];
[self.view addSubview:view2];
view2.frame = CGRectMake(CGRectGetMaxX(view1.frame) + 20, view1.frame.origin.y, view1.frame.size.width, view1.frame.size.height);
view2.backgroundColor = [UIColor redColor];
UIView * view3 = [[UIView alloc]init];
[self.view addSubview:view3];
view3.frame = CGRectMake(view1.frame.origin.x, CGRectGetMaxY(view1.frame) + 20, self.view.frame.size.width – 2 * padding, view1.frame.size.height);
view3.backgroundColor = [UIColor yellowColor];
}
代码写完,运行效果如下:
1
这里我在6与5s上都有运行过,都得到了我们想要的结果.当然是竖屏情况下,如果你横屏,就会发现变成这样,
2
其实不难理解,我们的代码已经尽可能的写的使其不用过多适配,当是从某方面说,这些frame还是死的数值,所以竖屏他就没有去适配.那么,如果我们使用masonry来编写代码呢…来试试看,代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int padding = 20;
UIView * view1 = [[UIView alloc]init];
[self.view addSubview:view1];
view1.backgroundColor = [UIColor greenColor];
UIView * view2 = www.hbbz08.com [[UIView alloc]init];
[self.view addSubview:view2];
view2.backgroundColor = [UIColor redColor];
UIView * view3 = [[UIView alloc]init];
[self.view addSubview:view3];
view3.backgroundColor = [UIColor yellowColor];
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(padding);
make.left.equalTo(self.view).with.offset(padding);
make.right.equalTo(view2.left).with.offset(-padding);
make.bottom.equalTo(view3.top).with.offset(-padding);
make.width.equalTo(view2);
make.height.equalTo(view3);
}];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(padding);
make.right.equalTo(self.view).with.offset(-padding);
make.width.equalTo(view1);
make.height.equalTo(view3);
}];
[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view).with.offset(padding);
make.right.equalTo(self.view).with.offset(-padding);
make.bottom.equalTo(self.view).with.offset(-padding);
make.height.equalTo(view1);
}];
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询