Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView created in storyboard initialized with frame 0,0,0,0

I created a UIScrollView in the storyboard, linked it as a property to the view controller, and am now trying to add some subviews with pagination. But, even though I set the dimensions in the storyboard graphically, the frame of the UIScrollView ends up being 0,0,0,0. Am I required to hardcode the dimensions even though I already specified them graphically in the storyboard?

Also, kind of related, is it possible to customize subviews in the storyboard to use in this scroll view? It seems that I cannot create such a view unless it is already within a controller. Since I only have one controller and potentially multiple views I want to set up graphically, how do I do this without having to do so programmatically?

like image 1000
user1096786 Avatar asked Jun 18 '12 21:06

user1096786


People also ask

How do I change the Scrollview content size in storyboard?

Step 1 : Add Scrollview to view in storyboard and add leading, trailing, top and bottom constraints (All values are zero). Step 2 : Don't add directly views which you need on directly scrollview, First add one view to scrollview (that will be our content view for all UI elements).

What is Scrollview in iOS?

Overview. UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView . A scroll view is a view with an origin that's adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the application's main window.


3 Answers

With storyboards viewDidLayoutSubviews is where you can get the size of all your views. viewWillAppear did not work for me and always returned (0,0,0,0);

like image 179
Ben Coffman Avatar answered Nov 22 '22 11:11

Ben Coffman


With storyboards the outlets of a controller are not connected before viewDidLoad and the size of it is not set until viewWillAppear, see here:

http://cs193p.m2m.at/lecture-8/

As soon as the controller is fully instantiated and its outlets are hooked up viewDidLoad: is called. Thus this is a good place to put setup code. However the size of controller is not set yet, but in viewWillAppear: which is called just before the view appears on screen.

So I think Enricos suggestion is correct. Just try putting the size-related stuff into viewWillAppear.

like image 32
Christian Hennen Avatar answered Nov 22 '22 11:11

Christian Hennen


I found out that when I access the scrollview in viewWillAppear, the frame will actually give the correct numbers. I previously tried this in awakeFromNib and viewDidLoad and got 0,0,0,0 frame as well.

What's weird is before Storyboard, you are able to get the correct frame dimensions in awakeFromNib. An example of this is in one of Apple's sample code PageControl.

So in short to answer the question, try accessing it from viewWillAppear(Assuming you created the UIScrollView in Storyboard).

like image 29
Enrico Susatyo Avatar answered Nov 22 '22 10:11

Enrico Susatyo