Here’s two ways to embed/use a UIScrollView for your iOS application.
1. Embedded UIScrollView from storyboard.
Say you have a really large image
and you want the user to be able to scroll around your phone with it.
One way (if you are using storyboards) is to add a UIImageView to your ViewController. Select the UIImage and change it’s ViewMode from Scale to fit -> Top Left).
And then embed your image in a ScrollView by going Editor -> Embed In -> ScrollView.
If you Shift-Right-Click on your image you should see a ScrollView element there now.
Now the only left is to set the content size of our scroll view area to be the same size as our big image.
You can do this by dragging over the scrollView and imageView as properties and then setting the content size in viewDidLoad like this:
- (void)viewDidLoad { [super viewDidLoad]; self.scrollView.contentSize = self.imageView.image.size; }
Now everything should scroll.
That’s probably the easiest way to setup a scrollable image.
2. Draw scrollview outside of ViewController and add after.
Now option#1 was good for scrolling images, but what if your scrollable design area is bigger than your Xcode ViewController area?
Now you don’t want the scrollable area nested in your view, because you won’t be able to see your design.
This option always you to keep your UIScrollView outside your view for design, and then add it after in the viewController like so:
- (void)viewDidLoad { [super viewDidLoad]; // set the content size to be the size our our whole frame self.scrollView.contentSize = self.scrollView.frame.size; // then set frame to be the size of the view's frame self.scrollView.frame = self.view.frame; // now add our scroll view to the main view [self.view addSubview:self.scrollView]; }
Big advantage of this option is it lets you see your design in Xcode in it’s entirety. Note this option was done with xibs – not storyboards (I havent figure out how to do this with storyboards yet).
But there you go. Two options for creating scrollviews.
A big thanks to Jeremy Gale (@jeremygale) for showing me option#2 with xibs (very handy).
Happy scrolling!
Chris
Jun 06, 2012 @ 23:32:22
Sorry for being a n00b here but how exactly do you do this –
“You can do this by dragging over the scrollView and imageView as properties and then setting the content size in viewDidLoad like this…”
How do I drag them over as properties exactly?
JR
Jun 07, 2012 @ 09:31:38
No worries Chris. It’s not obvious.
In iOS we call this a control – drag. That is you select the UI element you’d like to make a property (say a label). So click the label.
Then, while it’s selected, hold down the control key and drag the label over to the *.h or *.m file near the top where the properties go.
If all is well, Xcode will display an arrow as you are dragging, and it will transform into a property line of text when you let go over the objective file file.
This is how you created properties in your code from element in your view.
Let me know if this works for you.
Cheers – Jonathan
Ionut
Jul 13, 2012 @ 04:50:27
Hi Jonathan,
Loved your tutorial, exactly what I needed, but unfortunately I need to use it inside the storyboard. Figured it out how to do it?
I had some lame attempts, to add a new class with xib and then try to use it in storyboard but no success.
JR
Jul 16, 2012 @ 12:59:28
Not I haven’t. This is next on my hit list. Stay tuned as I want to crack this nut too.
Cheers – Jonathan
César
Sep 10, 2012 @ 14:30:15
Hello.
Can you “Draw scrollview outside of ViewController and add after” in a storyboard?
Thx!!
JR
Sep 11, 2012 @ 14:51:38
I don’t know Cesar. I think you can but I haven’t tried that yet on a storyboard (only xib). It’s on my todo list.
Enis
Oct 02, 2012 @ 21:16:19
TESTED – working solution to use Scroll View in storyboard: http://christinemorris.com/2011/07/ios-making-the-scrollview-scroll/
Just put these 2 lines in viewDidLoad method:
scrollView.frame = CGRectMake(74, 261, 620, 354);
// then define how much they can scroll it
[scrollView setContentSize:CGSizeMake(1220, 354)];
Jonathan Millar
Oct 28, 2012 @ 16:22:43
Hi There I followed your first example to the letter but my image does not scroll at all. mmmm cant figure out what Im doing wrong. All the steps you show I can successfully replicate but my simulator doesnt moce the image, Im trying it on ipad and not iPhone, is that the cause? Any suggestions?
Cheers
Jon
JR
Oct 29, 2012 @ 02:10:33
Hard to say without seeing the code Jonathan, but it could be that you iPad screen is so large that it doesn’t need to scroll.
One thing you could try is running the same code on an a much small iPhone screen. If it scrolled there you would know that it your problem.
Good luck!
deepjyoti2roy
Dec 19, 2012 @ 07:40:42
I have implemented a scrollView as described above using interface builder. My scrollView is much longer in height than the mainView. I want the scrollView to appear only in the bottom half of my main view (view1). Therefore I have created another sub-view(view2) and then added the scrollView to View2, simultaneously I have added View2 to View1. Now the scrollView is appearing in my main view, however I am not being able to scroll to the bottom of the scroll view. The scrollView scrolls to a certain limit, but not able to scroll to the bottom limit of the scroll view.
Somehow I found out that in interfaceBuilder if I increase the height of the scrollView at the bottom (leaving a huge blank space at the bottom of scrollView in interfaceBuilder) , then I am able to scroll everything !! , and the huge blank space also does not show up in the iOS simulator. !!!!!! I want to know where I am doing wrong. !!!!
Ross Philip
Dec 11, 2013 @ 17:41:03
Just for anyone having trouble making this work in iOS7. You need to select the top level View in the storyboard, use the File inspector, and uncheck “Use Autolayout”.
JR
Dec 11, 2013 @ 18:45:50
Thanks Ross.
aqavi
Apr 21, 2014 @ 12:35:12
Reblogged this on Coins Bazar.
Liroi
Aug 13, 2014 @ 07:38:49
hi
when zooming in the scroll view takes over and the image is driftting away and gets sucked into the scroller
what can i do ?
sun
Nov 03, 2014 @ 01:11:06
how degins scrollview long than viewcontroller like that above
zav
Nov 03, 2014 @ 19:24:47
In Xcode 5.1.1 targeting iOS 7. Followed your steps for the image and how can this work without adding properties for imageView and scrollView to the view controller?
Xcode barks about the properties not existing, so I added them as weak IBOutlets.
Then in the VDL, I added your content size code.
You state that “Now everything should scroll.”
Ran the app. The image displays, but no scrolling. No clue what is going wrong if this is all you have to do to enable scrolling.
Any ideas on the missing steps?
zav
Nov 03, 2014 @ 19:32:26
Solutions to “this image is not scrolling” are twofold:
1. disable AutoLayout on the storyboard
or
2. set the frame of the scrollView and the content size of the scrollView.