6

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?

user1096786
  • 95
  • 1
  • 7

5 Answers5

16

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);

Ben Coffman
  • 1,750
  • 1
  • 18
  • 26
  • 1
    Note also a trick is you can grab the sizes of the contained subviews there and then add layout code for those subviews' subviews there now knowing the frame/bounds. – John LaBarge Sep 10 '14 at 16:36
3

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.

1

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).

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
  • 1
    From `awakeFromNib` discussion: An awakeFromNib message is sent to each object loaded from the archive, but only if it can respond to the message, and only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet instance variables set. And from what I tested I get nil reference to my outlet in this method. So maybe this assumption is not true anymore with storyboard and the documentation need to be updated. Though I already have frame of my outlets set in `viewDidLoad`. – grandouassou Oct 15 '12 at 22:20
  • Excuse me, I have the exact problem for just trying to drag the UIScrollView to storyboard and the size inspector did defined its size. However, I set a breakpoint in `viewWillAppear`(not `viewDidLoad`!!) and it still state the frame is {0,0,0,0}. Although I can manually set back on it , what happen when the UIScrollView loaded from storyboard? – Yeung Oct 24 '12 at 07:15
  • Hey, I found out that I can access the correct frame value in `viewDidAppear`. I am building in Xcode 4.5 and for iPhone 6.0 Simulator. Isn't there something update cause this? – Yeung Oct 24 '12 at 07:47
  • @神 貓 that's basically what I was wondering. @florian thinks that it works differently in iOS 6 and the docs needs to be updated. – Enrico Susatyo Oct 25 '12 at 01:26
  • As you said, the solution was to use viewDidAppear (i was using viewWillAppear). THANK YOU SO MUCH. You saved me another day of headache. – NitroG42 Apr 10 '13 at 15:17
  • The scroll view frame is NOT set when `viewWillAppear` is called, but it is set when `viewDidAppear` is called. I personally consider this a bug and am going to go file one with Apple right now. I need to be able to layout my views before they are shown to the user, so I need that info in `viewWILLAppear`. – Kenny Wyland Aug 08 '13 at 04:57
  • @KennyWyland Please do. Before iOS 6 the frame was set in both methods, so I assume this is a bug that was introduced in iOS 6. – Enrico Susatyo Aug 08 '13 at 05:01
  • It's not a bug, its related to Autolayout. Under AL views don't have frames until a layout pass has completed. – jrturton Aug 09 '13 at 07:05
0

You probably set the breakpoint on a line where the view was not setup yet. Try this:

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        CGRect frame = self.scrollView.frame;
        NSLog (@"FRAME IS %@", NSStringFromCGRect(frame));
    }
    return self;
}
Alex L
  • 8,419
  • 6
  • 43
  • 51
0

It's quite simple: Disable Use Autolayout in the Storyboard settings.

Disable Use Autolayout in the Storyboard settings (red rectangle)

Michael Dorner
  • 17,587
  • 13
  • 87
  • 117