xib里 scrollView的 适配

http://blog.csdn.net/delegate_m/article/details/52022691



1、在使用xib定制界面时,我们经常会用到AutoLayout自动布局,或者不用AutoLayout,自己在xib属性的地方进行约束

2、xib中,ScrollView的自动约束并不像我们平常约束其他view一样,由于ScrollView的滚动范围是由有属性ContentSize决定的,所以在xib中使用AutoLayout约束的时候会相对杂一下。 
3、在xib中拖动一个scrollerView到xib中,scrollerView直接在View下面,界面结构如下: 
 
4、对srollerView进行约束,并更新位置 
这里写图片描述 
5、添加约束完成之后,会发现一切正常,并没有报错,接下来我们往srollerView中添加一个View,会发现立马报错,

这里写图片描述

点击小红点,会发现ContentSize报错,

这里写图片描述

我们不用管他,返回去就可以了,然后选中View,添加约束,

这里写图片描述

6、给view添加约束之后,会发现还是报错, 
这里写图片描述

这是需要给view再添加一个垂直约束(ScrollView需要垂直滚动,如果需要水平滚动,则需要水平约束,并设置view的宽约束), 
这里写图片描述

添加垂直约束相当于添加X约束,添加成功之后,会报Y没有约束的错误, 
这里写图片描述

然后我们再选中View,添加height约束,这里约束的是600, 
这里写图片描述

这是会发现,没有报错了,说明可以正常使用了

7、如果想要在控制器中动态的设置ScrollView的ContentSize,可以将view的高度拉线设置成属性,然后在控制器合适的计算地方设置ScrollView的垂直滚动范围,

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">weak</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">IBOutlet</span> NSLayoutConstraint *scrollerViewContentWith; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 属性设置</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.scrollerViewContentWith.constant = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1200</span>; <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span> 设置<span class="hljs-constant" style="box-sizing: border-box;">ScrollView</span>的垂直滚动范围</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

8、如果想要往ScrollView上添加需要跟随ScrollView滚动的控件(比如label),只需要将label添加到view(上面高度约束为600的那个view)上,成为view的subView就可以了,简单来说就是忽略掉ScrollView,把需要的控件放到view上,按照正常的约束来约束控件就可以了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章