之前使用Masonry对UIScrollView进行过约束,当时是遇到了问题的,怎么约束都不对,因为赶进度直接改用frame了也没有对问题深究。就这样过了很久.........,直到前一段换工作的时候面试官问到,使用Masonry对UIScrollView自动布局应该注意些什么?额....,犹豫了一段时间我只能搪塞说我一般都是用frame进行设置的,暂时没有遇到什么问题。虽然这么回答也没什么,但是感觉终归不是很好。出来混迟早是要还的!刚好最近公司不忙,想起了这个问题,那么就研究记录一下吧!
对UIScrollView的约束有很多方式,我只讲其中一种易懂、直观的方式
UIScrollView约束的关键是设置它的contentSize的大小,否则无法进行滚动显示。
给UIScrollView添加一个过渡视图containerView,这个containerView作为所有子控件的父视图,然后再设置containerView相对于UIScrollView的约束constraint和子控件相对于containerView的约束constraint。
最后再将最后一个子视图的右边距,或底边距设置成containerView的右边距,或底边距,以此来设置scrollView的contentSize在水平方向,或垂直方向的大小。
UIScrollView竖向滑动时,就把containerView的width固定
UIScrollView横向滑动时,就把containerView的height固定
如果看完上面的描述还没有明白,那直接看下面的代码,UIScrollView的约束其实都是一样的,记住即可
水平方向
UIScrollView *horizontalScrollView = [[UIScrollView alloc] init];
horizontalScrollView.backgroundColor = [UIColor orangeColor];
horizontalScrollView.pagingEnabled =YES;
// 添加scrollView添加到父视图,并设置其约束
[self.view addSubview:horizontalScrollView];
[horizontalScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.left.mas_equalTo(10);
make.right.mas_equalTo(-10);
make.height.mas_equalTo(100);
}];
// 创建过渡视图并设置contentSize和其约束
UIView *horizontalContainerView = [[UIView alloc] init];
[horizontalScrollView addSubview:horizontalContainerView];
[horizontalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(horizontalScrollView);
make.height.equalTo(horizontalScrollView);#//水平滚动高度固定,这个很重要
}];
//过渡视图上添加子视图
UIView *previousView =nil;
for (int i =0; i <10; i++) {
UILabel *label = [[UILabelalloc]init];
label.textAlignment =NSTextAlignmentCenter;
label.backgroundColor = [UIColorcolorWithHue:(arc4random() %256 / 256.0)
saturation:(arc4random() %128 /256.0) +0.5
brightness:(arc4random() %128 /256.0) +0.5
alpha:1];
label.text = [NSStringstringWithFormat:@"第 %d个视图", i];
//添加到过渡视图,并设置子视图的约束
[horizontalContainerView addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.and.bottom.equalTo(horizontalContainerView);
make.width.equalTo(horizontalScrollView);
if (previousView) {
make.left.mas_equalTo(previousView.mas_right);
}
else {
make.left.mas_equalTo(0);
}
}];
previousView = label;
}
#// 设置过渡视图的右距(此设置将影响到scrollView的contentSize)这个也是关键的一步
[horizontalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(previousView.mas_right);
}];
垂直平方向
UIScrollView *verticalScrollView = [[UIScrollView alloc] init];
verticalScrollView.backgroundColor = [UIColor greenColor];
verticalScrollView.pagingEnabled =YES;
// 添加scrollView添加到父视图,并设置其约束
[self.view addSubview:verticalScrollView];
[verticalScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10);
make.bottom.and.right.mas_equalTo(-10.0);
make.height.mas_equalTo(100);
}];
// 设置scrollView的子视图,即过渡视图contentSize,并设置其约束
UIView *verticalContainerView = [[UIView alloc] init];
[verticalScrollView addSubview:verticalContainerView];
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.and.right.equalTo(verticalScrollView);
make.width.equalTo(verticalScrollView);#//垂直滚动宽度固定,这个很重要
}];
//过渡视图添加子视图
UIView *lastView =nil;
for (NSInteger index =0; index <10; index++) {
UILabel *label = [[UILabelalloc]init];
label.textAlignment =NSTextAlignmentCenter;
label.backgroundColor = [UIColorcolorWithHue:(arc4random() %256 / 256.0)
saturation:(arc4random() %128 /256.0) +0.5
brightness:(arc4random() %128 /256.0) +0.5
alpha:1];
label.text = [NSStringstringWithFormat:@"第 %ld个视图", index];
//添加到过渡视图,并设置子视图的约束
[verticalContainerView addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(verticalContainerView);
make.height.mas_equalTo(verticalScrollView.mas_height);
if (lastView) {
make.top.mas_equalTo(lastView.mas_bottom);
}
else {
make.top.mas_equalTo(0);
}
}];
lastView = label;
}
#// 设置过渡视图的底边距(此设置将影响到scrollView的contentSize)这个也是关键的一步
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
xib对UIScrollerView的布局可以参考下面的文章
https://www.jianshu.com/p/1d3bb3cf7ee5
https://blog.csdn.net/dreams_deng/article/details/80523485
参考资料:https://blog.csdn.net/potato512/article/details/51226747
参考资料:https://www.jianshu.com/p/9948d917696b