以编程方式将滚动视图滚动到其底部

以编程方式将滚动视图滚动到其底部

本文介绍了以编程方式将滚动视图滚动到其底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have a scrollview and am trying to scroll to its bottom programmatically..

尝试了这些:

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)
    }
}

}

来自:

以编程方式滚动Swift 中 UIScrollView 到子 UIView(子视图)的顶部

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

来自:

UIScrollView 以编程方式滚动到底部

但两者都没有做任何事情......

but both didn't do anything ...

怎么做?

推荐答案

偏移设置不起作用,因为您尝试在生命周期的早期调用.

The offset setting doesn't works because you tried in calling early in the life cycle.

您可以尝试更新 viewDidLayoutSubviewsviewDidAppear

You could try updating the contentOffset at viewDidLayoutSubviews or viewDidAppear

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

这篇关于以编程方式将滚动视图滚动到其底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 08:24