Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView zoom with Autolayout

I know there are a couple of questions around but I could not figure out how to do this (none of the answers there helped me), so here is my version of the question.

I have an UICollectionView which is set to scroll horizontally. Each cell has an UIScrollView inside and inside the UIScrollView I have an UIImageView.

Now, the images displayed by the UIImageView are loaded from the internet and I can't figure out a way to make the zooming work correctly using storyboards and autolayout.

If I set constraints that tie my UIScrollView to it's container everything is ok. The moment I tie the UIImageView to the UIScrollView XCode starts to complain that the UIScrollView's size is ambiguous.

If I don't make any constraints in InterfaceBuilder the images are not the displayed in the correct size (doh!).

So, I'm stuck. I don't know what kind of constraints to make and which view needs to be tied to what. I know that my perfect world result is a view controller that behaves like the native iOS Photos app. That is, the image is displayed as large as the screen (with a black band at top and bottom or left & right depending on image's orientation) and that you can zoom it in and pan it around.

Help please!

Note

I did read these posts before posting my own question

  • UIScrollView zooming with Auto Layout
  • UIScrollView Zoom Does Not Work With Autolayout
  • “Pinch to Zoom” using AutoLayout
like image 926
Mihai Fratu Avatar asked Oct 02 '14 16:10

Mihai Fratu


1 Answers

I don't know if it'll help you with your zooming problem but Apple provided a technical note about using scroll views with auto layout: Technical Note TN2154, UIScrollView And Autolayout

The important part with scroll views is, that the constraints of the subviews inside the scroll view are not bound to the scroll view itself, but to the scroll view's parent (in your case the collection view cell)

like image 53
marcusficner Avatar answered Oct 21 '22 09:10

marcusficner