【问题标题】:UIButton at bottom of UIScrollView not workingUIScrollView 底部的 UIButton 不起作用
【发布时间】:2017-03-29 11:28:23
【问题描述】:

我的 UI 结构是这样的:

- View
  - Scroll View
    - Container View
      - Dynamically added UI element
      - Dynamically added UI element
      - Dynamically added UI element
      - ....
      - UIButton

添加所有 UI 元素(包括带有 UITapGestureRecognizer 的 UIButton)并使用以下方法更新 UIScrollView 大小后...

func updateScrollViewSize() {
    var contentRect = CGRect.zero
    for view in containerView.subviews {
        contentRect = contentRect.union(view.frame)
    }
    contentRect.size = CGSize(width: scrollView.contentSize.width, height: contentRect.height + TRAILING_SCROLLVIEW_SPACE)
    scrollView.contentSize = contentRect.size
}

...我注意到只有在视图顶部添加一个 UIButton 时它才会起作用 - 我只能通过滚动到达的那些(例如底部的那个)不起作用。

好吧,我想,所以按钮是在容器视图的框架之外创建的,因此我在updateScrollViewSize 方法中添加了以下行。

containerView.frame.size = contentRect.size

很遗憾,这并没有解决我的问题。

我尝试寻找解决方案已经有一段时间了,但没有人能帮到我。我仍然 99% 确定它与我的 Container Views 的框架有关,但我根本不知道如何解决这个问题。任何帮助将不胜感激!

【问题讨论】:

  • 尝试将容器视图的 -clipsBounds 设置为 true/yes。按钮是否仍然可见?
  • set contentSize of scrollView height = 最后添加 ui 组件的 y pos + height + 10 或相应的。
  • @isaac:不是。我使用print containerView.frame.bounds 对其进行了调试,并且高度根据内容发生了相应的变化。这是我访问的错误属性吗?

标签: ios swift uiscrollview uibutton


【解决方案1】:

如果使用任何约束,请在创建子视图后在顶视图上调用 layoutIfNeeded()。 确保滚动视图的 contentSize 与您的 containerView.frame 匹配(不是 .bounds,因为这不会告诉您例如您的框架 y 位置是否低于零。

【讨论】:

    猜你喜欢
    • 2015-03-23
    • 1970-01-01
    • 2013-05-15
    • 2012-11-08
    • 2015-10-07
    • 2018-06-01
    • 1970-01-01
    • 2015-03-05
    相关资源
    最近更新 更多