1. 程式人生 > >使用Masonry實現UILabel和UIIMageView自適應長度居中

使用Masonry實現UILabel和UIIMageView自適應長度居中

程式碼在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上進行約束。