Sti Sti - 1 year ago 107
Objective-C Question

How can I set the correct bounds to a view when the device rotates?

I have a programmatically created UIScrollView, and I am creating it as the size of the UIView behind it. In

I basically have:

UIScrollView *scrollView= [[UIScrollView alloc] initWithFrame:mainView.bounds];
//mainView being the UIView behind

When the device rotates, naturally the UIView in the background changes bounds to wider and shorter(though x,y ≠ y,x).

In my
I put `NSLog(@"%@", NSStringFromCGRect(mainView.bounds));, and the mainView's bounds has switched, but if I do this:

scrollView.bounds = mainView.bounds;
NSLog(@"%@", NSStringFromCGRect(scrollView.bounds));
NSLog(@"%@", NSStringFromCGRect(mainView.bounds));

It still shows up wrong..
The NSLog logs out the same bounds for both views, but on screen the mainView is correct, and the scrollView seems flipped or something, as if (0,0) in origin is outside the mainView, and the sizes are flipped wrong..
Do I have to redraw something, or am I calling all of this in the wrong place? Or isn't
the right variable to use?


I realize that updating the frame is different than re-initiating it with
, but I want the current content to stay the same, in the same way..

Answer Source

You need to set the scrollview's frame not it's bounds

scrollView.frame = mainView.bounds;

Or better yet if you set up the correct autoresizing masks/constraints it will resize automatically.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download