一、AutoLayout有两个重要的属性:
1.Content Compression Resistance 百度翻译(内容压缩抗力)
2.Content Hugging 百度翻译(内容拥抱)
二、解释
Content Compression Resistance = 变大!(保持自己更大)
这个属性的优先级(Priority)越高,越不“容易”被压缩。也就是说,当整体的空间装不下所有的View的时候,Content Compression Resistance优先级越高的,现实的内容越完整。
[self.labelTwo setContentCompressionResistancePriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
Content Hugging = 抱紧!(保持自己更小)
这个属性的优先级越高,整个View就要越“抱紧”View里面的内容。也就是View的大小不会随着父级View的扩大而扩大。
[self.labelOne setContentHuggingPriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
>参数一:(UILayoutPriority)
>设置优先级等级,数值越大,优先级越高。
>UILayoutPriorityRequired == 1000;
>UILayoutPriorityDefaultHigh == 750;
>UILayoutPriorityDefaultLow == 250;
>UILayoutPriorityFittingSizeLevel == 50;
>参数二:(UILayoutConstraintAxis)
>百度翻译(Axis:轴线。意思是你添加的优先级是Horizontal还是Vertical)
>UILayoutConstraintAxisHorizontal
>UILayoutConstraintAxisVertical
三、代码部分
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@property(nonatomic,strong) UILabel * labelOne;
@property(nonatomic,strong) UILabel * labelTwo;
@end
@implementation ViewController
- (void)initUI
{
[self.view addSubview:self.labelOne];
[self.labelOne mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).with.offset(20);
make.top.equalTo(self.view.mas_top).with.offset(20);
}];
[self.view addSubview:self.labelTwo];
[self.labelTwo mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_labelOne.mas_right);
make.right.equalTo(self.view.mas_right);
make.top.equalTo(self.view.mas_top).with.offset(20);
make.height.equalTo(_labelOne);
}];
[self.labelOne setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
/**参数一:(UILayoutPriority)
UILayoutPriorityRequired
UILayoutPriorityDefaultHigh
UILayoutPriorityDefaultLow
UILayoutPriorityFittingSizeLevel
*/
/**参数二:(UILayoutConstraintAxis)
UILayoutConstraintAxisHorizontal
UILayoutConstraintAxisVertical
*/
[self.labelTwo setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
}
- (UILabel *)labelOne{
if (!_labelOne) {
self.labelOne = [[UILabel alloc] init];
self.labelOne.text = @"这是labelOne";
self.labelOne.backgroundColor = [UIColor redColor];
}
return _labelOne;
}
- (UILabel *)labelTwo{
if (!_labelTwo) {
self.labelTwo = [[UILabel alloc] init];
self.labelTwo.text = @"这是labelTwo";
self.labelTwo.backgroundColor = [UIColor greenColor];
}
return _labelTwo;
}
@end
网友评论