I would like to display an image (width: 320 pixels, height: 1250 pixels) in an image view.
When I compile the application I get no scrolling at all. The view stays fixed.
What I did:
- Added an
UIScrollView
via Interface Builder to my view. - Added an
UIImageView
via Interface Builder to my view. - Verified that
UIImageView
is belowUIScrollView
in Interface Builder. - Set the size of the
UIScrollView
withViewDidLoad
.
How do I do this?
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView.contentSize = CGSizeMake(320, 1250);
}
Screenshots:
ImageView
:
ScrollView
:
So for me the problem was that setting the content size didn't work in
viewDidLoad()
. I tried everything and I didn't understand why it wouldn't want to work, and then I tried the same stuff inviewDidAppear()
and it magically worked...I came across this same issue on iOS6 and the solution was to programmatically adjust the ContentSize. So I will just quote from Raja (above) to show this:
NOTE: I was not having this issue on iOS5.. seems like iOS6 decided to do alot of prank just like the rotation/orientation saga
I found I had a similar problem but none of the above code worked. The issue was due to autolayout. I found that if I turned off autolayout by going to the storyboard clicking on Utilities -> File Inspector and unchecked
Use Autolayout
the scrolling did work (I also setscroll.contentSize = ...
).Just add code
to method -(void)viewDidLayoutSubviews. For more information checkout Stanford CS193p Lecture No 8 to understand View Controller Life cycle.