我使用Masonry在代码中创建自动布局约束。
这是我到目前为止所拥有的:
使用以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
UIView *container = [[UIView alloc] init];
[self.view addSubview:container];
UIView *itemContainer = [[UIView alloc] init];
[itemContainer setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
[container addSubview:itemContainer];
UIImageView *itemImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Test"]];
[itemContainer addSubview:itemImage];
UILabel *itemTitle = [[UILabel alloc] init];
[itemTitle setNumberOfLines:1];
[itemTitle setText:@"Lorem ipsum dolor sit amet."];
[itemTitle setFont:[UIFont boldSystemFontOfSize:12]];
[itemTitle setTextColor:[UIColor blackColor]];
[itemContainer addSubview:itemTitle];
UILabel *itemText = [[UILabel alloc] init];
[itemText setNumberOfLines:2];
[itemText setText:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt"];
[itemText setTextColor:[UIColor blackColor]];
[itemText setFont:[UIFont systemFontOfSize:10]];
[itemContainer addSubview:itemText];
[container makeConstraints:^(MASConstraintMaker *make) {
UIView *topLayoutGuide = (id)self.topLayoutGuide;
make.top.equalTo(topLayoutGuide.bottom);
make.left.right.bottom.equalTo(self.view).insets(UIEdgeInsetsMake(10, 10, 10, 10));
}];
[itemContainer makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(container);
make.height.equalTo(@80);
}];
[itemImage makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.left.equalTo(itemContainer);
make.width.equalTo(itemImage.height);
}];
[itemTitle makeConstraints:^(MASConstraintMaker *make) {
make.top.right.equalTo(itemContainer);
make.left.equalTo(itemImage.right).offset(5);
}];
[itemText makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(itemTitle);
make.top.equalTo(itemTitle.bottom).offset(5);
}];
}
现在我想将标签垂直居中,如下所示:
到目前为止,我的方法都失败了。知道如何通过自动布局/砌体实现这一目标吗?
- 编辑
建议使用像 chris838 这样的间隔视图是有效的。这是更新的代码:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
UIView *container = [[UIView alloc] init];
[self.view addSubview:container];
UIView *itemContainer = [[UIView alloc] init];
[itemContainer setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
[container addSubview:itemContainer];
UIImageView *itemImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Test"]];
[itemContainer addSubview:itemImage];
UIView *spacer1 = [[UIView alloc] init];
[itemContainer addSubview:spacer1];
UIView *spacer2 = [[UIView alloc] init];
[itemContainer addSubview:spacer2];
UILabel *itemTitle = [[UILabel alloc] init];
[itemTitle setNumberOfLines:1];
[itemTitle setText:@"Lorem ipsum dolor sit amet."];
[itemTitle setFont:[UIFont boldSystemFontOfSize:12]];
[itemTitle setTextColor:[UIColor blackColor]];
[itemContainer addSubview:itemTitle];
UILabel *itemText = [[UILabel alloc] init];
[itemText setNumberOfLines:2];
[itemText setText:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt"];
[itemText setTextColor:[UIColor blackColor]];
[itemText setFont:[UIFont systemFontOfSize:10]];
[itemContainer addSubview:itemText];
[container makeConstraints:^(MASConstraintMaker *make) {
UIView *topLayoutGuide = (id)self.topLayoutGuide;
make.top.equalTo(topLayoutGuide.bottom);
make.left.right.bottom.equalTo(self.view).insets(UIEdgeInsetsMake(10, 10, 10, 10));
}];
[itemContainer makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(container);
make.height.equalTo(@80);
}];
[itemImage makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.left.equalTo(itemContainer);
make.width.equalTo(itemImage.height);
}];
[spacer1 makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(itemContainer.top);
make.height.equalTo(spacer2);
}];
[itemTitle makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(spacer1.bottom);
make.right.equalTo(itemContainer);
make.left.equalTo(itemImage.right).offset(5);
}];
[itemText makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(itemTitle);
make.top.equalTo(itemTitle.bottom).offset(5);
make.bottom.equalTo(spacer2.top);
}];
[spacer2 makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(itemContainer.bottom);
make.height.equalTo(spacer1);
}];
}