Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling autorotate for a single UIView

Okay, this seems like it should be relatively simple, but I've been Googling for the better part of an hour, and can't seem to find what I need.

I have a view controller that has a few different parts: a background view, a header view, and a few buttons. Now, I want the header and buttons to autorotate properly (they do, when I return YES from shouldAutorotateToInterfaceOrientation:), but under no circumstances should the background view rotate. Is there a proper way to do this?

like image 242
Cocoatype Avatar asked Feb 03 '10 11:02

Cocoatype


2 Answers

If you don't want temporary rotations of the background view after which the background is changed to have the correct orientation, you'll need to turn off Autorotation for the View. No two ways about it.

You would need to handle the rotation of the buttons yourself. You can either make a nice layout or put them on for example a Scrollview and just resize that.

Either way, you'd need to add an observer to rotate it yourself,

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];

And in didRotate you do the resize, with a nice animated transition, if you like.

- (void) didRotate:(NSNotification *)notification {
 int ori=1;
    UIDeviceOrientation currOri = [[UIDevice currentDevice] orientation];
    if ((currOri == UIDeviceOrientationLandscapeLeft) || (currOri == UIDeviceOrientationLandscapeRight)) ori=0;
}

Hope that sorts you. If Navigation bars or status bars cause the View to get tucked in under the top bar, there are ways to fix that.

like image 125
Henrik Erlandsson Avatar answered Sep 19 '22 02:09

Henrik Erlandsson


There is another work around that I've thought about...

You could use the:

bgView.transform = CGAffineTransformMakeRotate(angle);

You should calculate the angle according to the orientation (it could be M_PI/2, M_PI or 3*M_PI/2)...

You can use this function inside - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration and also wrap it with an animation that will animate for the exact duration as the screen orientation animation:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    // Decide what will be the angle...

    // Make the animation
    [UIView beginAnimations:@"orientationChange" context:nil];
    [UIView setAnimationDuration: duration];
    bgView.transform = CGAffineTransformMakeRotate(angle);
    [UIView commitAnimations];
}

Notice that view's size might be changed because of the top bar...

Notice also that I've never tried it myself...

like image 26
Michael Kessler Avatar answered Sep 22 '22 02:09

Michael Kessler