我怎么能让一个UIScrollView滚动到我的代码底部?或者用更一般的方式,到子视图的任意点?


当前回答

正如这里所解释的 https://janeshswift.com/ios/swift/how-to-scroll-to-a-position-programmatically-in-uiscrollview/

我们可以创建一个自定义UIScrollView扩展为

extension UIScrollView {
    
    func scrollToTop(animated: Bool = false) {
        setContentOffset(CGPoint(x: contentOffset.x, y: -adjustedContentInset.top), animated: animated)
    }
    
    var bottomContentOffsetY: CGFloat {
        max(contentSize.height - bounds.height + adjustedContentInset.bottom, -adjustedContentInset.top)
    }
    
    func scrollToBottom(animated: Bool = false) {
        setContentOffset(CGPoint(x: contentOffset.x, y: bottomContentOffsetY), animated: animated)
    }
    
    func scrollToLeading(animated: Bool = false) {
        setContentOffset(CGPoint(x: -adjustedContentInset.left, y: contentOffset.y), animated: animated)
    }
    
    var trailingContentOffsetX: CGFloat {
        max(contentSize.width - bounds.width + adjustedContentInset.right, -adjustedContentInset.left)
    }
    
    func scrollToTrailing(animated: Bool = false) {
        setContentOffset(CGPoint(x: trailingContentOffsetX, y: contentOffset.y), animated: animated)
    }
    
    func scrollViewToVisible(_ view: UIView, animated: Bool = false) {
        scrollRectToVisible(convert(view.bounds, from: view), animated: true)
    }
    
    var isOnTop: Bool {
        contentOffset.y <= -adjustedContentInset.top
    }
    
    var isOnBottom: Bool {
        contentOffset.y >= bottomContentOffsetY
    }
    
}

把它当作——

DispatchQueue.main.async {
    self.itemsScrollView.scrollToBottom()
}

其他回答

确保内容底部可见的一个好方法是使用以下公式:

contentOffsetY = MIN(0, contentHeight - boundsHeight)

这确保内容的下边缘始终位于视图的下边缘或高于视图的下边缘。MIN(0,…)是必需的,因为UITableView(可能还有UIScrollView)确保当用户试图通过可见的抓取contentOffsetY = 0来滚动时,contentOffsetY >= 0。对于用户来说,这看起来很奇怪。

实现这个的代码是:

UIScrollView scrollView = ...;
CGSize contentSize = scrollView.contentSize;
CGSize boundsSize = scrollView.bounds.size;
if (contentSize.height > boundsSize.height)
{
    CGPoint contentOffset = scrollView.contentOffset;
    contentOffset.y = contentSize.height - boundsSize.height;
    [scrollView setContentOffset:contentOffset animated:YES];
}
CGFloat yOffset = scrollView.contentOffset.y;

CGFloat height = scrollView.frame.size.height;

CGFloat contentHeight = scrollView.contentSize.height;

CGFloat distance = (contentHeight  - height) - yOffset;

if(distance < 0)
{
    return ;
}

CGPoint offset = scrollView.contentOffset;

offset.y += distance;

[scrollView setContentOffset:offset animated:YES];

Xamarin的。iOS版本的UICollectionView的接受答案,便于复制和粘贴

var bottomOffset = new CGPoint (0, CollectionView.ContentSize.Height - CollectionView.Frame.Size.Height + CollectionView.ContentInset.Bottom);          
CollectionView.SetContentOffset (bottomOffset, false);

类别来拯救!

将此添加到共享实用程序头的某处:

@interface UIScrollView (ScrollToBottom)
- (void)scrollToBottomAnimated:(BOOL)animated;
@end

然后是那个实用程序实现:

@implementation UIScrollView(ScrollToBottom)
- (void)scrollToBottomAnimated:(BOOL)animated
{
     CGPoint bottomOffset = CGPointMake(0, self.contentSize.height - self.bounds.size.height);
     [self setContentOffset:bottomOffset animated:animated];
}
@end

然后在任何你喜欢的地方执行它,例如:

[[myWebView scrollView] scrollToBottomAnimated:YES];

使用UIScrollView的setContentOffset:animated:函数滚动到Swift的底部。

let bottomOffset : CGPoint = CGPointMake(0, scrollView.contentSize.height - scrollView.bounds.size.height + scrollView.contentInset.bottom)
scrollView.setContentOffset(bottomOffset, animated: true)