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


当前回答

扩展UIScrollView添加一个scrollToBottom方法:

extension UIScrollView {
    func scrollToBottom(animated:Bool) {
        let offset = self.contentSize.height - self.visibleSize.height
        if offset > self.contentOffset.y {
            self.setContentOffset(CGPoint(x: 0, y: offset), animated: animated)
        }
    }
}

其他回答

我还发现了另一种有用的方法,在这种情况下,你正在使用UITableview(这是UIScrollView的子类):

[(UITableView *)self.view scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

迅速:

   if self.mainScroll.contentSize.height > self.mainScroll.bounds.size.height {
        let bottomOffset = CGPointMake(0, self.mainScroll.contentSize.height - self.mainScroll.bounds.size.height);
        self.mainScroll.setContentOffset(bottomOffset, animated: true)
    }

扩展UIScrollView添加一个scrollToBottom方法:

extension UIScrollView {
    func scrollToBottom(animated:Bool) {
        let offset = self.contentSize.height - self.visibleSize.height
        if offset > self.contentOffset.y {
            self.setContentOffset(CGPoint(x: 0, y: offset), animated: animated)
        }
    }
}

看起来这里所有的答案都没有考虑到安全区域。 从iOS 11开始,iPhone X引入了安全区域。这可能会影响scrollView的contentInset。

对于iOS 11及以上版本,正确地滚动到底部,包括插入的内容。你应该使用adjustedContentInset而不是contentInset。检查这段代码:

迅速:

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

objective - c

CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height + self.scrollView.adjustedContentInset.bottom);
[self.scrollView setContentOffset:bottomOffset animated:YES];

Swift扩展(这保留了原来的contenttoffset .x):

extension UIScrollView {
    func scrollsToBottom(animated: Bool) {
        let bottomOffset = CGPoint(x: contentOffset.x,
                                   y: contentSize.height - bounds.height + adjustedContentInset.bottom)
        setContentOffset(bottomOffset, animated: animated)
    }
}

引用:

adjustedContentInset

正如这里所解释的 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()
}