3

I made a scrollview in my storyboard which contains severals UIImageView.

The problem is, the frame of that scrollview is equal to {0, 0, 0, 0} and I don't know why. The scrollview is visible on my screen, but I'm not able to scroll it.

I already try to set a content size, and a frame, but without success.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
Vinestro
  • 1,072
  • 1
  • 10
  • 32
  • What are you doing to detect the frame size? If it's visible, then the odds are that the {0, 0, 0, 0} one is not the one you're seeing (or, at least, not while you're seeing it). Also, how is the controller that contains the scroll view being created? – Phillip Mills Oct 20 '12 at 22:47
  • To detect the frame size, I do an: NSLog(@"%@", NSStringFromCGRect(self.theScrollView.frame)); in the viewDidLoad method – Vinestro Oct 20 '12 at 22:49
  • Although this is a later question, it is the same as this one: http://stackoverflow.com/questions/11090999/scrollview-created-in-storyboard-initialized-with-frame-0-0-0-0 The answer presented worked for me (using viewDidLayoutSubviews instead of view[Will|Did]Appear. – Jeremy Wiebe May 28 '13 at 00:07

6 Answers6

8

Fixed it out !

In fact, I develop the app on iOS 6.0 and I had to uncheck the "use autolayout" checkbox on storyboard properties...

Thanks to all anyway !

Vinestro
  • 1,072
  • 1
  • 10
  • 32
4

I was getting this problem too but I need to use auto layout.

I was trying to set an UIImageView as a subview then do some calcs to work out the minimum, maximum zoom scales and set the zoomScale.

The problem was that I was trying to set zoomScale to zero as the scroll views frame was zero which was giving me this error:

<Error>: CGAffineTransformInvert: singular matrix.

So I setup the image and imageView in viewDidLoad / viewWillAppear (and hide the imageView) and then set the zoomScale in viewDidAppear (then unhide imageView).

This seems to work well, although I can't say for sure why. I guess it gives the scroll view a chance to set its bounds correctly.

Ants
  • 1,338
  • 16
  • 27
4

The issue here is AutoLayout, as some of the other answers have indicated. Specifically, the issue is that autolayout does not occur until after viewWillAppear, and if you put your code in viewDidAppear, you will get funky display artifacts as you change things on the screen while the user is watching...

If you need AutoLayout enabled, the best place to put your frame dependent code is in:

- (void)viewDidLayoutSubviews

Keep in mind that this gets called again and again though, so if you only want some initialization code run once, when the view is displayed, then you can create a BOOL property that stores whether the initial layout has already been done.

@property (assign, nonatomic) BOOL initialLayoutComplete;

And then you can write your viewDidLayoutSubviews this way:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    if (!self.initialLayoutComplete) {
        // frame dependent code here...
    }
}
Scott Allen
  • 1,199
  • 1
  • 12
  • 14
3

As I mentioned in this answer: The frame of the scrollview will not be initialised until you are in the viewWillAppear method.

This seems to be a new behaviour in iOS 6, not sure if it's a bug or an intentional change.

You should be able to use auto layout without worrying about this.

Community
  • 1
  • 1
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
0

Did you connect IBOutlet?

Did you init scrollView with this code:

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 280, 360)];
self.scrollView.contentSize = CGSizeMake(500, 360);
//values are of course only example values

How you add UIImageView to scrollView?

edzio27
  • 4,126
  • 7
  • 33
  • 48
  • Yes I connected the IBOutlet. I tried to init the scrollview but I have the same problem. Finally, I had my UIImageViews with the drag'n'drop way, in storyboard too. Thanks in advance ! – Vinestro Oct 20 '12 at 23:11
0

I think one clarification is in place to Enrico comment below. The frame size will have the right values at viewDidAppear (and in viewWillAppear the value will be set, but they are still zero in that method)

Zeev Vax
  • 914
  • 7
  • 13