zack_falcon zack_falcon - 5 months ago 50
iOS Question

Xcode / Swift Scrollview Layout too wide

I'm part of a team working on an app that uses auto-layout for the iPhone 6, but we just found out that we also need to support iPhone 4s and 5. Because the layout was designed for 6's larger screen, we decided to use ScrollViews.

I used this video as a tutorial of sorts:

UIScrollView and Autolayout - Xcode 6

For the most part, it works. I can scroll up and down.

The Problem:

Scrolling vertically works. But now I also have to scroll horizontally, which is not ideal / shouldn't happen.

Even with the simulator set back to iPhone 6, anything with the scrollview gets widened to the point that the user has to scroll sideways.

Any suggestions?

What I Tried:

Some parts of the app does work without a hitch as far as my eye can see, but I've yet to apply a scrollview to those, so it's hard to tell if it's the scrollview's fault or something else at work. I did find this, though:

Non-working (scrollviews used - can scroll down, but width gets blown up):

enter image description here

Working (no scrollviews used - can't scroll down, but UI matches screen width):

enter image description here

I have no idea if that actually makes a difference. I tried to set the size to any/any as well, but that screws up with existing auto-layout constraints.

I also tried doing it programatically, but there's some known issues regarding the system returning the wrong screen size (which screws up everything even worse).

Answer

When working with ScrollView & AutoLayout you should put all content in a content view(UIView) and pin it to scroll view. (I am assuming this you did)

Now, If you want to disable horizontal scroll(like in TableView) then you will need to add a equal width constraint between scroll view & content view.