对UIScrollView使用Auto Layout,scrollView不能滑动的根本原因是该 scollView 的 contentSize 计算错误。
在使用Auto Layout时,UIScrollView会根据子视图的大小和约束来动态计算contentSize。我们需要做的就是告诉系统怎么去计算。
博主采用Masonry进行自动布局,代码如下:
//......scrollView的各个 子view
//scrollView位于底部的 子view
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(tips.mas_bottom).offset(20);
make.centerX.equalTo(bottomView.superview.mas_centerX);
make.width.equalTo(bottomView.superview.mas_width).offset(-20);
make.height.equalTo(@(bottomHeight));
}];
这个时候scrollView就不能滑动,因为系统虽然知道了各个子view的大小和相互之间的约束,但却不知道子view与scrollView之间的约束。也就是说,没法通过子view去计算scrollView的contentSize。修正的方法是加一行代码
make.bottom.equalTo(bottomView