ios - 以编程方式将 ScrollView 滚动到其底部

标签 ios swift xcode

我有一个scrollview,正在尝试以编程方式滚动到其底部。

尝试了这些:

extension UIScrollView {

// Bonus: Scroll to bottom
func scrollToBottom() {
    let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
    if(bottomOffset.y > 0) {
        setContentOffset(bottomOffset, animated: true)
    }
}

}

从:

Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift
  let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
  scrollView.setContentOffset(bottomOffset, animated: true)

从:

UIScrollView scroll to bottom programmatically

但是两个都没做...

怎么做?

最佳答案

偏移量设置无效,因为您尝试在生命周期的早期进行调用。

您可以尝试通过contentOffsetviewDidLayoutSubviews更新viewDidAppear

let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
 scrollView.setContentOffset(bottomOffset, animated: true)

关于ios - 以编程方式将 ScrollView 滚动到其底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50832747/

相关文章:

objective-c - Objective - C 静态库和它的公共(public)头文件 - 什么是正确的方法?

ios - 传递数据时如何使一个变量可以被多个类识别?

ios - Facebook sdk编译错误

ios - SwiftUI View 无法正确呈现为UIImage

xcode - xcode 中的 cocos2d 通过名为 socket.io 的 Objective-C 库与 Node.js 服务器服务进行通信

ios - 为 iOS 构建后 Ionic 应用程序链接器错误

iphone - AFNetworking 停止下载具有相同 url 的图像

javascript - YouTube 事件嵌入到 Mobile Safari、iPhone(停止时回调)

ios - UIPageViewController 结合安全区域

swift - 避免在 deinit 期间第一次访问引用 self 的惰性变量时崩溃