Vinestro
Vinestro

Reputation: 1082

UIScrollView made in storyboard has frame value of zero

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.

Upvotes: 3

Views: 2580

Answers (6)

Scott Allen
Scott Allen

Reputation: 1189

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

Upvotes: 4

Zeev Vax
Zeev Vax

Reputation: 924

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)

Upvotes: 0

Enrico Susatyo
Enrico Susatyo

Reputation: 19790

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.

Upvotes: 3

Ants
Ants

Reputation: 1338

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.

Upvotes: 4

Vinestro
Vinestro

Reputation: 1082

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 !

Upvotes: 8

edzio27
edzio27

Reputation: 4140

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?

Upvotes: 0

Related Questions