Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView - Need Constraints for x position/width, Need Constraints for y position/height

I have a view hierarchy that looks like this (based on other answers and Apple's advanced AutoLayout guide for working with UIScrollView):

enter image description here

The 2 steps required for ScrollView are:

  1. Set up constraints for position and size (frame) of ScrollView: The same way you do it with any other view.
  2. Set up constraints for content size: By making sure that there are constraints from the ScrollView's subviews touching all edges (leading, trailing, top, bottom) of the ScrollView, if you're doing this in interface builder and not programmatically.

Step 1 worked fine at first and this was the result:

enter image description here

enter image description here

No problems with position and size as expected. I just need to define the content size (content height and content width) now in Interface Builder with Step 2.

Now I add the constraints touching all 4 edges of the ScrollView like so:

enter image description here

Suddenly, the ScrollView doesn't know it's position and size (frame) anymore.

enter image description here

I've looked at other answers and followed the various steps, but can't seem to resolve this issue. Sorry I'm not able to post Storyboard screenshots because of privacy issues

like image 938
Fourth Avatar asked May 15 '17 22:05

Fourth


People also ask

How do I add a height constraint to a storyboard?

Select the view you would like to constrain. Then tap the button to the right of the one you have selected and use that menu to define your autolayout constraints. If you want it to work for all devices make sure your storyboard is on the wAny hAny size class setting.

What is constraint to margin?

The “Constrain to margins” checkbox determines whether constraints to the superview use the superview's margins or its edges. The lower portion of the popover lets you set the item's width or height. The Width and Height constraints default to the current canvas size, though you can type in different values.

How do I make my stack view scrollable?

Add a UIStackView to the UIScrollView. Set the constraints: Leading , Trailing , Top & Bottom should be equal to the ones from UIScrollView. Set up an equal Width constraint between the UIStackView and UIScrollView . Set Axis = Vertical, Alignment = Fill, Distribution = Equal Spacing, and Spacing = 0 on the UIStackView.


1 Answers

  1. Set the scroll view's top, bottom, leading, trailing constraints to its superview to 0.

  2. Set the view that is inside the scroll view and set its top, bottom, leading, trailing constraints to its superview to 0.

  3. Select the view that is inside the scroll view, go to the size inspector and set the "Intrinsic size" to "Placeholder".

like image 114
hkdalex Avatar answered Oct 03 '22 16:10

hkdalex