I have a view inside a scrollview whose content changes, so its size has to change too and if necessary, the scrollview has to be able to scroll.
This is my Storyboard
At the beginning, the Lv Height Constraint says that Lv should be as high as its superview.
Now, every time the content changes I execute this code (I imported the constraint as an outlet):
//Content is updated...
//Determines wether scrolling is enabled by comparing the size of the content and the size of the super view
var heightMulti: CGFloat
if(superviewHeight!.isLess(than: svYPos)) {
heightMulti = svYPos/superviewHeight! //svYPos is the height of the content
scrollView.isScrollEnabled = true
} else {
heightMulti = 1
scrollView.isScrollEnabled = false
}
//Sets height of content view
lvHeightConstraint.constant = heightMulti
//Scrolls to the top of the view again
scrollView.scrollToTop()
But somehow, the height doesn't change, so I can not scroll.
This code does work in 99% of the cases, but I am just adding constraints, which is a bad approach as far as I know
//Determines wether scrolling is enabled by comparing the size of the content and the size of the super view
var heightMulti: CGFloat
if(superviewHeight!.isLess(than: svYPos)) {
heightMulti = svYPos/superviewHeight!
scrollView.isScrollEnabled = true
} else {
heightMulti = 1
scrollView.isScrollEnabled = false
}
//Sets height of content view
lv.heightAnchor.constraint(equalTo: superview.heightAnchor, multiplier: heightMulti).isActive = true
//Scrolls to the top of the view again
scrollView.scrollToTop()