视图中如果还有视图 使用cgrectMake计算坐标的时候 会很麻烦于是 找到了masonry 非常好用 下面写一些简单的示范
首先下载一个masonry 在viewController .h文件中引入Masonry,h文件即可
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor whiteColor];
UIView *sv = [UIView new];
sv.backgroundColor = [UIColor redColor];
[self.view addSubview:sv];
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
UIView *sv1 = [UIView new];
[sv addSubview:sv1];
sv1.backgroundColor = [UIColor yellowColor];
[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
/* 等价于
make.top.equalTo(sv).with.offset(10);
make.left.equalTo(sv).with.offset(10);
make.bottom.equalTo(sv).with.offset(-10);
make.right.equalTo(sv).with.offset(-10);
*/
/* 也等价于
make.top.left.bottom.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
*/
}];
int padding1 = 10;
UIView *sv2 = [UIView new];
sv2.backgroundColor = [UIColor blueColor];
UIView *sv3 = [UIView new];
sv3.backgroundColor = [UIColor blueColor];
[self.view addSubview:sv2];
[self.view addSubview:sv3];
[sv2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(sv.mas_centerY);
make.left.equalTo(sv.mas_left).with.offset(padding1);
make.right.equalTo(sv3.mas_left).with.offset(-padding1);
make.height.mas_equalTo(@150);
make.width.equalTo(sv3);
}];
[sv3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(sv.mas_centerY);
make.left.equalTo(sv2.mas_right).with.offset(padding1);
make.right.equalTo(sv.mas_right).with.offset(-padding1);
make.height.mas_equalTo(@150);
make.width.equalTo(sv2);
}];
// 让两个高度为150 的view垂直居中且等宽等间隔排列 间隔为10(自动计算宽度)
self.view.backgroundColor = [UIColor orangeColor];
UIView *sv = [UIView new];
sv.backgroundColor = [UIColor redColor];
[self.view addSubview:sv];
// Masonry的autolauout添加函数 将所需要的约束添加到block中
[sv mas_makeConstraints:^(MASConstraintMaker *make) {
// 居中
make.center.equalTo(self.view);
// 设置宽高
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
UIScrollView *scrollView = [UIScrollView new];
scrollView.backgroundColor = [UIColor whiteColor];
[sv addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(5, 5, 5, 5));
}];
UIView *container = [UIView new];
[scrollView addSubview:container];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.width.equalTo(scrollView);
}];
int count = 10;
UIView *lastView = nil;
for (int i = 1; i <= count; ++i) {
UIView *subView = [UIView new];
[container addSubview:subView];
subView.backgroundColor = [UIColor colorWithHue:arc4random() % 256 / 256.0 saturation:arc4random() %128 / 256.0 + 0.5 brightness:arc4random() % 128 / 256.0 + 0.5 alpha:1];
[subView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(container);
make.height.mas_equalTo(@(20 * i));
if (lastView) {
make.top.mas_equalTo(lastView.mas_bottom);
} else {
make.top.mas_equalTo(container.mas_top);
}
}];
lastView = subView;
}
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
}