有时候需要把一些很简单的页面做成可滑动的,比如这个简单的修改昵称的页面。

UITableView
来实现显得有点笨重。所以我的做法是直接把ViewController
底层的view
(也就是self.view
)替换成UIScrollView
。
1.初步试探
在loadView
方法里将self.view
替换成UIScrollView
,并且设置alwaysBounceVertical
为YES
。
//将self.view替换成UIScrollView,使页面可以滑动
- (void)loadView {
UIScrollView *scrollView = [UIScrollView new];
scrollView.alwaysBounceVertical = YES;
self.view = scrollView;
}
2.布局问题
但是此时子view
的布局出现了问题。如截图所示:

我是使用
Masonry
写的布局,其中textField
的左边和self.view
的左边对其,textField
的右边和self.view
的右边对其,如下代码所示:
[self.view addSubview:mTextField];
[mTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top);
make.left.equalTo(self.view.mas_left);
make.right.equalTo(self.view.mas_right);
make.height.equalTo(@(TEXTFIELD_HEIGHT));
}];
但是很明显,我的textField
并没有达到右对其的效果。
3.查找原因
后来在网上查到了原因,同时也埋怨自己对UIScrollView
理解的不深入。
UIScrollView依靠与其subviews之间的约束来确定ContentSize的大小。
基于这篇文章(UIScrollview与Autolayout的那点事),我修改了自己的代码,如下:

稍微麻烦一点的是,以后不能再将view
添加到self.view
上,需要将其添加到containerView
上。所以我添加了一个属性:
@property (nonatomic, strong) UIView *containerView;
并且实现了它的Getter
方法:
- (UIView *)containerView {
return [self.view.subviews firstObject];
}
以后就将view
添加到self.containerView
上并且设置约束就行了:
[self.containerView addSubview:mTextField];
[mTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.containerView.mas_top);
make.left.equalTo(self.containerView.mas_left);
make.right.equalTo(self.containerView.mas_right);
make.height.equalTo(@(TEXTFIELD_HEIGHT));
}];
4.还有一点别的
以前实现一些长度超过屏幕范围的UIScrollView
时,总是需要手动计算并设置contentSize
。
按照现在这种方法,就不必再计算contentSize
了,只需要从上到下依次将子view
添加进去并且设置好约束就可以了,UIScrollView
会自己依据与子view
的约束来计算contentSize
。
5.最后再重复一遍
UIScrollView依靠与其subviews之间的约束来确定ContentSize的大小。
其实其他情况也类似,在用自动布局实现UI时,若是不手动设置frame
,view
也会根据内容或者子view
的大小来确定自己的frame
。
网友评论