知识点:
一.AutoLayout有两个重要的属性:
1.Content Compression Resistance 百度翻译(内容压缩抗力)
2.Content Hugging 百度翻译(内容拥抱)
Content Compression Resistance = 变大!(保持自己更大)
这个属性的优先级(Priority)越高,越不“容易”被压缩。也就是说,当整体的空间装不下所有的View的时候,Content Compression Resistance优先级越高的,现实的内容越完整。
[self.labelTwosetContentCompressionResistancePriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
Content Hugging = 抱紧!(保持自己更小)
这个属性的优先级越高,整个View就要越“抱紧”View里面的内容。也就是View的大小不会随着父级View的扩大而扩大。
[self.labelOnesetContentHuggingPriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
UIViewAutoresizingNone 不会随父视图的改变而改变
UIViewAutoresizingFlexibleLeftMargin 自动调整view与父视图左边距,以保证右边距不变
UIViewAutoresizingFlexibleWidth 自动调整view的宽度,保证左边距和右边距不变
UIViewAutoresizingFlexibleRightMargin 自动调整view与父视图右边距,以保证左边距不变
UIViewAutoresizingFlexibleTopMargin 自动调整view与父视图上边距,以保证下边距不变
UIViewAutoresizingFlexibleHeight 自动调整view的高度,以保证上边距和下边距不变
UIViewAutoresizingFlexibleBottomMargin 自动调整view与父视图的下边距,以保证上边距不变
纯代码实现cell布局,利用约束实现自适应布局。
下面展示的为实现自适应输入框的部分关键代码
[图片上传中...(image-89a2f0-1569465337493-0)]
初始化cell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self updateViewConstraint];
}
return self;
}
设置布局的约束条件
- (void)setupViewConstraint {
[self.labelBeizhu mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.labelPriceTip.mas_leading);
make.top.mas_equalTo(self);
make.height.mas_equalTo(@(44));
make.width.mas_equalTo(@(50));
}];
[self.textViewBeiZhu mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.labelBeizhu.mas_trailing).offset(10);
make.top.mas_equalTo(self).offset(6);
make.height.mas_equalTo(@(32));
make.trailing.mas_equalTo(self).offset(-10);
make.bottom.mas_equalTo(self).priorityLow();
}];
}
textView的属性设置
- (UITextView *)textViewBeiZhu {
if (_textViewBeiZhu == nil) {
UITextView *textView = [[UITextView alloc] init];
textView.font = [UIFont systemFontOfSize:15];
textView.backgroundColor = [UIColor whiteColor];
textView.showsVerticalScrollIndicator = NO;
textView.scrollEnabled=NO;
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_textViewBeiZhu = textView;
[self addSubview:_textViewBeiZhu];
}
return _textViewBeiZhu;
}
在textViewDidChange中实时计算textview的高度并更新
- (void)textViewDidChange:(UITextView *)textView {
//计算textview高度
CGRect frame = textView.frame;
CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
CGSize size = [textView sizeThatFits:constraintSize];
[self.textViewBeiZhu mas_updateConstraints:^(MASConstraintMaker *make) {
//更新height
make.height.mas_equalTo(@(size.height));
}];
// 重点
[self.mtableView beginUpdates];
[self.mtableView endUpdates];
}
</article>
7人点赞
日记本
](https://img.haomeiwen.com/i8280836/931961be1e342d26.png?imageMogr2/auto-orient/strip|imageView2/2/w/556)
初始化cell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self updateViewConstraint];
}
return self;
}
设置布局的约束条件
- (void)setupViewConstraint {
[self.labelBeizhu mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.labelPriceTip.mas_leading);
make.top.mas_equalTo(self);
make.height.mas_equalTo(@(44));
make.width.mas_equalTo(@(50));
}];
[self.textViewBeiZhu mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.labelBeizhu.mas_trailing).offset(10);
make.top.mas_equalTo(self).offset(6);
make.height.mas_equalTo(@(32));
make.trailing.mas_equalTo(self).offset(-10);
make.bottom.mas_equalTo(self).priorityLow();
}];
}
textView的属性设置
- (UITextView *)textViewBeiZhu {
if (_textViewBeiZhu == nil) {
UITextView *textView = [[UITextView alloc] init];
textView.font = [UIFont systemFontOfSize:15];
textView.backgroundColor = [UIColor whiteColor];
textView.showsVerticalScrollIndicator = NO;
textView.scrollEnabled=NO;
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_textViewBeiZhu = textView;
[self addSubview:_textViewBeiZhu];
}
return _textViewBeiZhu;
}
在textViewDidChange中实时计算textview的高度并更新
- (void)textViewDidChange:(UITextView *)textView {
//计算textview高度
CGRect frame = textView.frame;
CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
CGSize size = [textView sizeThatFits:constraintSize];
[self.textViewBeiZhu mas_updateConstraints:^(MASConstraintMaker *make) {
//更新height
make.height.mas_equalTo(@(size.height));
}];
// 重点
[self.mtableView beginUpdates];
[self.mtableView endUpdates];
}
网友评论