Change content size UIScrollView dynamically

764 Views Asked by At

I have a scroll view, when the view is first loaded, the size is set dynamically, but when I click on the button, the internal size of my elements changes and I need to change the internal size of the scroll, but it does not change. Someone knows how to fix it?

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

There are 2 best solutions below

0
On

This solution is for auto-layout/constraints. You need a reference constraint to manipulate the height of the inner container view of the scrollview.

private var _constraintInnerContainerScroll:NSLayoutConstraint?

You need to set the initial height of the inner container view, suppose 700.0

private let _containerViewHeightFixed    : CGFloat = 700.0

then you need to save the reference

_constraintInnerContainerScroll = _containerView.heightAnchor.constraint(equalToConstant: _containerViewHeightFixed)
_constraintInnerContainerScroll?.isActive = true

You initial view is setup and ready, now suppose you add 2 more subview of height 100.0 each, now your new inner container view height should be 700.0+200.0 = 900.0

    if let const1 = _constraintInnerContainerScroll{
            const1.constant = _containerViewHeightFixed + 200.0
            UIView.animate(withDuration: 0.5) {            
           self?._containerView.layoutIfNeeded()
    }else{
           print("constraint reference not saved")
    }

let me know if this works for you, or if this can be improved.

0
On

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