方法
1、将UIScrollView添加到UIView控件中,设置UIScrollView针对父视图UIView的constraints(Leading/trailling/top/bottom = 0)或者根据自己需要设置相应数值。
2、在UIScrollView中添加UIView控件,起名ContentView。
UIScrollView是靠子视图来实现autolayout的高度和宽度的计算。设置ContentView的针对父视图UIScrollView的 constraints
Leading/trailling/top= 0;
bottom >= 1 ;
equal height / equal width (ContentView的高度和宽度与 UIScrollView相等);
设置equal height的优先级 priority小于 刚才设置的bottom的优先级(该bottom的是ContentView针对UIScrollView的constraints),这里设置priority=999.
分析:
为什么 bottom =1 而不是 bottom = 0。
如果为0,并且ContentView中的内容高度不超过屏幕的高度时,uiscrollview不能弹动。
要实现ContentView 的高度自适应为uiscrollview的contentSize的高度,需要ContentView中的子视图有明确的top与bottom,用来让autolayout计算出ContentView的实际高度.
为什么要设置equal height 的优先级?
因为 scrollview.height = uiview.height;
ContentView.height = scrollview.height + 1;
所以 ContentView.height != scrollView.height 也就是不能实现equal height这个constraints。会报错。只要调整一下优先级就可以解决这个问题,出现冲突,优先适应ContentView的高度
ContentView中如果使用UILabel,要给uilabel明确的height constraint,这样auto layout才能计算出高度。
例子
http://download.youkuaiyun.com/detail/bravegogo/9609124
参考:
http://blog.youkuaiyun.com/kmyhy/article/details/39929117
http://my.oschina.net/ioslighter/blog/390385
http://www.kittenyang.com/autolayoutforscrollview/
http://dadage456.blog.163.com/blog/static/30310744201491141752716