【问题标题】:Detecting if a UIScrollView has the height to scroll检测 UIScrollView 是否具有要滚动的高度
【发布时间】:2018-09-21 19:11:05
【问题描述】:

我有一个 UIScrollView,它有时有足够的 contentHeight 滚动,有时没有。我还有一个基于用户滚动到滚动视图底部的按钮。

如果用户不采取任何行动,我如何检测滚动视图是否具有要滚动的 contentHeight 以便我可以适当地设置按钮的默认 isEnabled?

谢谢!

【问题讨论】:

  • 如果您的所有内容都在顶级内容视图中(应该),那么您只需检查 contentView 的高度是否大于 scrollView 的高度。

标签: swift uiscrollview uikit


【解决方案1】:

你可以在 UIScrollView 上创建一个扩展

extension UIScrollView {
  var contentUntilBotttom: CGFloat {
    return max(contentSize.height - frame.size.height - contentOffset.y, 0)
  }

  var isAtTheBottom: Bool {
    return contentUntilBotttom == 0
  }
}

如果你使用 RxSwift,你可以观察到如下变化:

extension Reactive where Base == UIScrollView {
  var isAtTheBottom: ControlEvent<Bool> {
    let source = contentOffset
      .map({ _ -> Bool in
        return self.base.isAtTheBottom
      })
    return ControlEvent(events: source)
  }
}

// So then subscribe to it
scrollView.rx.isAtTheBottom
  .subscribe(onNext: { (isAtTheBottom) in
    // Update anithing you need
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-07
    • 2019-03-13
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 2016-06-29
    相关资源
    最近更新 更多