Sti Sti - 1 month ago 8
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

viewDidAppear
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
-(void)didRotateFromInterfaceOrientation:
I put `NSLog(@"%@", NSStringFromCGRect(mainView.bounds));, and the mainView's bounds has switched, but if I do this:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
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
.bounds
the right variable to use?

EDIT

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

Answer

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.