I have a stackview (which the user can add/remove elements from) inside of a scrollview and I want the scrollview always adjust to have the same height as the stackview so that I don't have to add a bunch of unnecessary space if there's only a few elements in the stackview. Is there any way to make the scrollview's height change to match the stackview's height whenever an element is added to the stackview?
Asked
Active
Viewed 414 times
3 Answers
0
You can use anchors like so,
ScollView.translatesAutoresizingMaskIntoConstraints =
false
ScrollView.heightanchor.constrains(equalTo :
stackview.heightconstrains).isActive = true
Another way if you don't want using auto layout anchors
Let height = stackView.frame.size.height
ScrollView.frame.size.height = height
Let me know if it solved your issue
-
Neither of those is working for me - the first one doesn't work because once a new constraint is set it conflicts with the original one. For the second one, I don't know why it doesn't work, it just doesn't change the size of the scrollview. – dfisch Apr 27 '20 at 01:22
-
Post you UI code please – Apr 27 '20 at 11:19
0
you need just manage the control inside the stackview scrollview automatically adapt the height accordingly.

Anil Kumar
- 1,830
- 15
- 24
0
I'm not sure why but I couldn't get any method of editing the scrollview to work, so what I did was make a function that removes the previous scrollview and adds a new one whenever new content is added to the view.

dfisch
- 11
- 5