問題描述
我在 Interface Builder 中創(chuàng)建了一個 UIScrollView 并為其添加了子視圖,但我無法讓它滾動.我看過其他類似的問題,但我已經(jīng)適當(dāng)?shù)卦O(shè)置了我的 contentSize.事實(shí)上,我什至嘗試過設(shè)置非常大的 contentSize,但它沒有產(chǎn)生任何影響.我錯過了什么?注意 UIScrollView 是一個 IBOutlet:
I have created a UIScrollView and added subviews to it in Interface Builder, but I am not able to get it to scroll. I have looked at other similar questions, but I have set my contentSize appropriately. In fact, I have even tried setting incredibly large contentSize and it fails to make a difference. What am I missing? Note that the UIScrollView is an IBOutlet:
@property (strong, nonatomic) IBOutlet UIScrollView *scroll;
這是在我看來DidLoad:
This is in my viewDidLoad:
self.scroll.scrollEnabled = YES;
[self.scroll setContentSize: CGSizeMake(2400,8000)];
這是來自 Interface Builder 的屏幕截圖:
Here are screenshots from Interface Builder:
這是運(yùn)行時的視圖,但在任何時候水平或垂直滾動??的任何嘗試都不起作用.
Here is the view while running, but any attempt at horizontal or vertical scrolling at any point does not work.
推薦答案
關(guān)閉自動布局可以,但這不是解決方案.如果你真的需要Auto Layout,那就使用它,如果你不需要它,把它關(guān)掉.但這不是此解決方案的正確解決方案.
Turning Auto Layout off works, but that's not the solution. If you really need Auto Layout, then use it, if you don't need it, turn it off. But that is not the correct fix for this solution.
UIScrollView
與自動布局中的其他視圖不同.這是 Apple 的關(guān)于 Auto 的發(fā)行說明布局,我復(fù)制了有趣的部分(強(qiáng)調(diào)我的,在第三個要點(diǎn)):
UIScrollView
works differently with other views in Auto Layout. Here is Apple's release note on Auto Layout, I've copied the interesting bit (emphasis mine, at third bullet point):
以下是有關(guān) UIScrollView 的自動布局支持的一些說明:
Here are some notes regarding Auto Layout support for UIScrollView:
- 通常,自動布局將視圖的頂部、左側(cè)、底部和右側(cè)邊緣視為可見邊緣.也就是說,如果您將視圖固定到它的超級視圖的左邊緣,你真的把它固定在超級視圖邊界的最小 x 值.更改邊界原點(diǎn)父視圖的位置不會改變視圖的位置.
- UIScrollView 類通過更改其邊界的原點(diǎn)來滾動其內(nèi)容.為了使這項(xiàng)工作與自動布局一起使用,頂部、左側(cè)、底部、滾動視圖中的右邊緣現(xiàn)在意味著其內(nèi)容的邊緣查看.
- 對滾動視圖的子視圖的約束必須導(dǎo)致要填充的大小,然后將其解釋為滾動視圖的內(nèi)容大小滾動視圖.(這不應(yīng)與用于自動布局的 intrinsicContentSize 方法.)調(diào)整滾動的大小具有自動布局的視圖框架,約束必須是顯式的關(guān)于滾動視圖的寬度和高度,或滾動視圖必須綁定到其子樹之外的視圖.
- 請注意,您可以通過創(chuàng)建約束使?jié)L動視圖的子視圖看起來浮動(不滾動)在其他滾動內(nèi)容之上在視圖和滾動視圖的子樹之外的視圖之間,例如滾動視圖的超級視圖.
Apple 然后繼續(xù)展示如何正確使用 UIScrollView
和 Auto Layout 的示例.
Apple then goes on to show example of how to correctly use UIScrollView
with Auto Layout.
作為一般規(guī)則,最簡單的解決方法之一是在元素到 UIScrollView 底部之間創(chuàng)建一個約束.因此,在您希望位于 UIScrollView 底部的元素中,創(chuàng)建此底部空間約束:
As a general rule, one of the easiest fix is to create a constraint between the element to the bottom of the UIScrollView. So in the element that you want to be at the bottom of the UIScrollView, create this bottom space constraint:
再一次,如果您不想使用自動布局,請將其關(guān)閉.然后,您可以按通常的方式設(shè)置 contentSize
.但你應(yīng)該明白,這是 Auto Layout 的預(yù)期行為.
Once again, if you do not want to use Auto Layout, then turn it off. You can then set the contentSize
the usual way. But what you should understand is that this is an intended behaviour of Auto Layout.
這篇關(guān)于無論內(nèi)容大小如何,UIScrollView 都不會滾動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!