gpt4 book ai didi

ios - 无论内容大小如何,UIScrollView 都不会滚动

转载 作者:技术小花猫 更新时间:2023-10-29 10:57:05 26 4
gpt4 key购买 nike

我已经在 Interface Builder 中创建了一个 UIScrollView 并向其添加了 subview ,但是我无法让它滚动。我看过其他类似的问题,但我已经适本地设置了我的 contentSize。事实上,我什至尝试过设置非常大的 contentSize,但没有任何效果。我错过了什么?请注意,UIScrollView 是一个 IBOutlet:

@property (strong, nonatomic) IBOutlet UIScrollView *scroll;

这是在我的 viewDidLoad 中:

self.scroll.scrollEnabled = YES;
[self.scroll setContentSize: CGSizeMake(2400,8000)];

以下是 Interface Builder 的屏幕截图:

view heirarchy

scrollview and its subviews in interface builder

referencing outlet

这是运行时的 View ,但任何时候水平或垂直滚动​​的尝试都不起作用。

screen while running

最佳答案

关闭自动布局可行,但这不是解决方案。如果你真的需要Auto Layout,那就用它,如果你不需要,就关掉它。但这不是此解决方案的正确解决方案。

UIScrollView 与自动布局中的其他 View 的工作方式不同。这是 Apple's release note在自动布局上,我复制了有趣的部分(强调我的,在第三个要点):

Here are some notes regarding Auto Layout support for UIScrollView:

  • In general, Auto Layout considers the top, left, bottom, and right edges of a view to be the visible edges. That is, if you pin a view to the left edge of its superview, you’re really pinning it to the minimum x-value of the superview’s bounds. Changing the bounds origin of the superview does not change the position of the view.
  • The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view.
  • The constraints on the subviews of the scroll view must result in a size to fill, which is then interpreted as the content size of the scroll view. (This should not be confused with the intrinsicContentSize method used for Auto Layout.) To size the scroll view’s frame with Auto Layout, constraints must either be explicit regarding the width and height of the scroll view, or the edges of the scroll view must be tied to views outside of its subtree.
  • Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.

然后 Apple 继续展示如何正确使用 UIScrollView 和自动布局的示例。

作为一般规则,最简单的解决方法之一是在元素与 UIScrollView 底部之间创建一个约束。因此,在您希望位于 UIScrollView 底部的元素中,创建此底部空间约束:

enter image description here

再次重申,如果您不想使用自动布局,请将其关闭。然后您可以按照通常的方式设置 contentSize。但您应该了解的是,这是自动布局的预期行为。

关于ios - 无论内容大小如何,UIScrollView 都不会滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21273585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com