我有一个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
但是两个都没做...
怎么做?
最佳答案
偏移量设置无效,因为您尝试在生命周期的早期进行调用。
您可以尝试通过contentOffset
或viewDidLayoutSubviews
更新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/