之前使用Masonry對(duì)UIScrollView進(jìn)行過(guò)約束非驮,當(dāng)時(shí)是遇到了問(wèn)題的,怎么約束都不對(duì)箍土,因?yàn)橼s進(jìn)度直接改用frame了也沒(méi)有對(duì)問(wèn)題深究肉拓。就這樣過(guò)了很久.........鸭丛,直到前一段換工作的時(shí)候面試官問(wèn)到竞穷,使用Masonry對(duì)UIScrollView自動(dòng)布局應(yīng)該注意些什么?額....鳞溉,猶豫了一段時(shí)間我只能搪塞說(shuō)我一般都是用frame進(jìn)行設(shè)置的瘾带,暫時(shí)沒(méi)有遇到什么問(wèn)題。雖然這么回答也沒(méi)什么熟菲,但是感覺(jué)終歸不是很好看政。出來(lái)混遲早是要還的朴恳!剛好最近公司不忙,想起了這個(gè)問(wèn)題允蚣,那么就研究記錄一下吧于颖!
對(duì)UIScrollView的約束有很多方式,我只講其中一種易懂嚷兔、直觀的方式
UIScrollView約束的關(guān)鍵是設(shè)置它的contentSize的大小森渐,否則無(wú)法進(jìn)行滾動(dòng)顯示。
給UIScrollView添加一個(gè)過(guò)渡視圖containerView谴垫,這個(gè)containerView作為所有子控件的父視圖章母,然后再設(shè)置containerView相對(duì)于UIScrollView的約束constraint和子控件相對(duì)于containerView的約束constraint。
最后再將最后一個(gè)子視圖的右邊距翩剪,或底邊距設(shè)置成containerView的右邊距,或底邊距彩郊,以此來(lái)設(shè)置scrollView的contentSize在水平方向前弯,或垂直方向的大小。
UIScrollView豎向滑動(dòng)時(shí)秫逝,就把containerView的width固定
UIScrollView橫向滑動(dòng)時(shí)恕出,就把containerView的height固定
如果看完上面的描述還沒(méi)有明白,那直接看下面的代碼违帆,UIScrollView的約束其實(shí)都是一樣的浙巫,記住即可
水平方向
UIScrollView *horizontalScrollView = [[UIScrollView alloc] init];
horizontalScrollView.backgroundColor = [UIColor orangeColor];
horizontalScrollView.pagingEnabled =YES;
// 添加scrollView添加到父視圖,并設(shè)置其約束
[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);
}];
// 創(chuàng)建過(guò)渡視圖并設(shè)置contentSize和其約束
UIView *horizontalContainerView = [[UIView alloc] init];
[horizontalScrollView addSubview:horizontalContainerView];
[horizontalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(horizontalScrollView);
make.height.equalTo(horizontalScrollView);#//水平滾動(dòng)高度固定刷后,這個(gè)很重要
}];
//過(guò)渡視圖上添加子視圖
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個(gè)視圖", i];
//添加到過(guò)渡視圖的畴,并設(shè)置子視圖的約束
[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;
}
#// 設(shè)置過(guò)渡視圖的右距(此設(shè)置將影響到scrollView的contentSize)這個(gè)也是關(guān)鍵的一步
[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添加到父視圖,并設(shè)置其約束
[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);
}];
// 設(shè)置scrollView的子視圖尝胆,即過(guò)渡視圖contentSize丧裁,并設(shè)置其約束
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);#//垂直滾動(dòng)寬度固定,這個(gè)很重要
}];
//過(guò)渡視圖添加子視圖
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個(gè)視圖", index];
//添加到過(guò)渡視圖含衔,并設(shè)置子視圖的約束
[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;
}
#// 設(shè)置過(guò)渡視圖的底邊距(此設(shè)置將影響到scrollView的contentSize)這個(gè)也是關(guān)鍵的一步
[verticalContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom);
}];
xib對(duì)UIScrollerView的布局可以參考下面的文章
http://www.reibang.com/p/1d3bb3cf7ee5
https://blog.csdn.net/dreams_deng/article/details/80523485
參考資料:https://blog.csdn.net/potato512/article/details/51226747
參考資料:http://www.reibang.com/p/9948d917696b