0

我有一个滚动视图,第一次加载视图时,大小是动态设置的,但是当我点击按钮时,我的元素的内部大小会发生变化,我需要更改滚动的内部大小,但它没有改变。有人知道如何解决吗?

       DispatchQueue.main.async {
        var contentRect = CGRect()
        for view in self.scrollView.subviews {
            contentRect = contentRect.union(view.frame)
            self.scrollView.contentSize = contentRect.size
        }
}
4

1 回答 1

0

If you really don't want to use auto-layout / constraints, you can call this function each time you add (or remove) a subview from the scroll view, or after you've changed the size(s) of the subview(s):

func updateContentSize() -> Void {
    // this will get the right-edge of the right-most subview
    let width = scrollView.subviews.map {$0.frame.maxX}.max() ?? 0.0

    // this will get the bottom-edge of the bottom-most subview
    let height = scrollView.subviews.map {$0.frame.maxY}.max() ?? 0.0

    // set the contentSize
    scrollView.contentSize = CGSize(width: width, height: height)
}
于 2021-08-24T17:07:11.917 回答