1
votes

I have a UIScrollView holding a ContentView, with that ContentView holding other interchangeable UIViews. Can get the multiple UIViews to show, however one of the UIViews have dynamic content, so changes its height if you click on a button, can also shrink back.

The structure looks like this - UIScrollView -> ContentView(UIView) -> UIView : enter image description here

I cannot get the Autolayout anchors correct, the content size doesn't change when the UIView changes height (button click). So I can scroll to see the growth in content, but its not clickable anymore, the extra growth height is the same height as what is not clickable (the ContentView didn't change its height)

Current setup:

ScrollView: Anchored at 0 to parent view all around

ContentView: Anchored at 0 to Scroll View all around. Height and width bound to Parent View.

UIViews: Anchored at 0 to ContentView all around.


Any ideas what I can change to fix this issue?

Side note: I won't be against removing the ContentView, but couldn't get it working either way.

1
The content view should not have an equal height constraint to its parent view...André Slotta
@AndréSlotta It doesn't make sense to me, but thats what a lot of tutorial do for Auto layout in scrollview. What do I equal its height to? I tried constant heights but that didn't workcfl
The content view does not get any kind of explicit height constraint. Instead you have to make sure that all the views within your content view have valid constraints from the very top of your content view to the very bottom so that it can calculate its own size.André Slotta
@AndréSlotta Thank you for your assistance. The views inside the content view are xibs, the only constraint I couldn't get is between the last view element(eg uibutton) of the xib(s) and the bottom of the actual xib. Doesn't seem to allow me, makes the whole view weird. But will try focus on that, see if that will resolve anythingcfl
@AndréSlotta Thank you so much - I got it working. The 'complete' height constraint in the xib was the missing piece.cfl

1 Answers

4
votes

I've setup the things as you set.

Scrollview-> Content view-> subview1
                           |-> subview2
                              |-> btn
                              |-> lbl

I've given following constraints...

  • For the scrollview, I've given top, bottom, leading and trailing constraints to its superview.

  • For content view (inside scrollview), I've given top, bottom, leading and trailing constraints to its scrollview.

  • For subview1 (inside content view), I have given top, leading, trailing and height constraints to content view.

  • For subview2 (inside content view and below subview1), I have given top, bottom, leading and trailing constraints to content view.

  • For label (inside subview2), I have given top, bottom, leading and trailing constraints to subview2.

  • For button (inside subview2), I've given trailing and top constraints to subview2.

See the pic.

enter image description here

Final output:

enter image description here