During UI development, two Uilabels will be displayed side by side, but the screen width is limited, so there may be a case that not all of them can be displayed. At this time, we need to consider how to set constraint layout.
There are two ways to do it
Use greaterThanOrEqualTo, lessThanOrEqualTo. You need to provide a definitive value
Using setContentHuggingPriority: forAxis: setContentCompressionResistancePriority: forAxis:
- SetContentHuggingPriority: forAxis: tensile, the lower the value, will be under the condition of the width of excess, is drawn.
- SetContentCompressionResistancePriority: forAxis: resistant to compression, the lower the value, will be under the condition of the width is not enough, be compressed.
@interface ViewController () @property (nonatomic, strong) UILabel *lLabel; @property (nonatomic, strong) UILabel *rLabel; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupViews]; } - (void)setupViews { self.view.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:self.lLabel]; [self.view addSubview:self.rLabel]; [self.lLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.offset(16); make.top.offset(100); make.trailing.mas_equalTo(self.rLabel.mas_leading).offset(-20); }]; [self.rLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.trailing.offset(-16); make.top.offset(100); }]; / / / / setContentHuggingPriority tensile, the lower the value, will be under the condition of the width of excess, is drawn. / / / / setContentCompressionResistancePriority compression resistance, the lower the value, will be under the condition of the width is not enough, be compressed. // display all lLabel content (if lLabel content is too long, RLabel won't show) / / [self lLabel setContentHuggingPriority: (UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)]; // [self.rLabel setContentCompressionResistancePriority:(UILayoutPriorityFittingSizeLevel) forAxis:(UILayoutConstraintAxisHorizontal)]; // display all lLabel content (if lLabel content is too long, RLabel won't show) / / [self lLabel mas_updateConstraints: ^ (MASConstraintMaker * make) {/ / make. Width. GreaterThanOrEqualTo (@ 0); / /}]; // //rLabel full display (if rLabel content is too long, LLabel won't show) / / [self lLabel setContentHuggingPriority: (UILayoutPriorityFittingSizeLevel) forAxis:(UILayoutConstraintAxisHorizontal)]; // [self.rLabel setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)]; // Display all rLabel (if rLabel content is too long, LLabel won't show) / / [self rLabel mas_updateConstraints: ^ (MASConstraintMaker * make) {/ / make. Width. GreaterThanOrEqualTo (@ 0); / /}]; [self.llabel mas_updateConstraints:^(MASConstraintMaker *make) {// make.width.greaterThanOrEqualTo(@100); // }]; // [self.rLabel mas_updateConstraints:^(MASConstraintMaker *make) { // make.width.greaterThanOrEqualTo(@100); // }]; // // here specifies who compresses, Don't look at the order/write / [self lLabel setContentCompressionResistancePriority: (UILayoutPriorityFittingSizeLevel) forAxis:(UILayoutConstraintAxisHorizontal)]; // //lLabel set maximum width // [self.llabel mas_updateConstraints:^(MASConstraintMaker *make) {// make.width.lessThanOrEqualTo(@100); // }]; // //rLabel set maximum width // [self.rlabel mas_updateConstraints:^(MASConstraintMaker *make) {// make.width.lessThanOrEqualTo(@100); // }]; self.lLabel.text = @"asdasdajladdawwwdasadadadsadasdadakladdn"; self.rLabel.text = @"addhjkhkhkhkhkjsdsaadaad"; } - (UILabel *)lLabel { if (! _lLabel) { _lLabel = [UILabel new]; _lLabel.textColor = [UIColor greenColor]; _lLabel.backgroundColor = [UIColor orangeColor]; } return _lLabel; } - (UILabel *)rLabel { if (! _rLabel) { _rLabel = [UILabel new]; _rLabel.textColor = [UIColor redColor]; _rLabel.textAlignment = NSTextAlignmentRight; _rLabel.backgroundColor = [UIColor blueColor]; } return _rLabel; } @endCopy the code