使用Masonry實現UILabel和UIIMageView自適應長度居中
阿新 • • 發佈:2019-02-08
程式碼在github上:demo
先看個效果圖:
主要程式碼:
[self.containerOne mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.labelTitle.mas_bottom).offset(20);
make.centerX.mas_equalTo(0);
make.leading.mas_greaterThanOrEqualTo(5);
make.trailing.mas_lessThanOrEqualTo(-5 );
}];
[self.iconOne mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.equalTo(self.containerOne);
make.width.mas_equalTo(20);
make.height.mas_equalTo(20);
make.centerY.mas_equalTo(0);
make.left.mas_equalTo(2);
}];
[self.labelOne mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.iconOne.mas_right).offset(5);
make.centerY.mas_equalTo(0);
make.right.mas_equalTo(-2);
}];
為了實現圖示和文字整體居中,需要將圖示和文字放到一個容器view上進行約束。