iOS - Using Storyboard and Autolayout to Center the Uiscrollview

iOS - Using storyboard and autolayout to center the UIScrollView

You will need to do this by adding an additional view to the screen.

At the moment you have...

- UIView (main view)
|
| - scrollView
| - tableView

You should put the scroll view inside another view like this...

- UIView (main view)
|
| - UIView (spacer View)
| | - scrollView
|
| - tableView

Now what you can do is have these constraints...

spacer view leading edge constraint to super view = 0
spacer view trailing edge to table view leading edge = 0
table view width = (whatever the width is)
table view trailing edge to super view = 0

This will lay out the spacer view and the table view so that the spacer view will grow.

Now you need to add...

scroll view width = x
scroll view height = y
scroll view centered vertically in super view
scroll view centered horizontally in super view.

Now, because the scroll view's super view is the spacer view then it will always be centered in between the table view and the rest of the space.

Centering a variable width column of text in a UIScrollView using Auto Layout

This one is actually pretty straightforward! I've included a storyboard file as a demo. The key bits for the text inside of the content view:

  • Leading/Trailing Margins >= 10
  • Width >= 300
  • Width <= 500
  • Center Align

No priority changes are required, unless you have other content that is going to take precedence.

Sample Image

iOS UIScrollView with autolayout not centering correctly

OK. So I think I lead you astray with my initial comment about the equal widths constraint - that is needed to stop the content view from expanding and taking your labels with it.

I am still not sure why it was collapsing to zero though.

Take a look at this SO question which seems to cover a similar issue. Do your constraints match theirs?

Edit - actually this answer may be clearer.

IOS/Storyboard/Autolayout. Center horizontal group of elements

This scenario is very common and very simple to solve: just get rid of the leading and trailing constraints and instead add horizontalCenter to the first label regarding the view. After that all you gotta do is adding horizontalCenter to the second one regarding the first and you're set! Now both of them are linked together and well centered.

If you need more help with this, just share your repo and I'll fork it.

How can I use Autolayout to set constraints on my UIScrollview?

It's hard to see the exact values and setup of your constraints as you've pasted them here, so I'm not sure from looking at your screenshots where you have gone wrong.

In lieu of an explanation of what's wrong in your setup, I've created a basic sample project with a very similar view hierarchy and constraint setup to the one you describe. The horizontal scrolling works as expected in the sample project, which uses the "Pure AutoLayout" approach that Apple describes in the Technical Note.

I also had a lot of trouble originally getting Auto Layout to work with UIScrollView. The key to getting it to work is making sure that all of the items in the scroll view, taken together, have constraints that eventually link to all sides of the scroll view and that contribute to the AutoLayout system being able to determine a contentSize for the scroll view that will be bigger than its frame. It looks like you were trying to do that in your code, but maybe you had some superfluous constraints in there that were making the contentSize too small.

Also of note, as others mentioned, with AutoLayout and UIScrollview, you no longer set the contentSize explicitly. The AutoLayout System calculates the contentSize based on your constraints.

I also found this ebook chapter to be very helpful in making me understand how all this works. Hope all this helps.



Related Topics



Leave a reply



Submit