yun.cloud yun.cloud - 5 months ago 13
iOS Question

Two Separate UIView Rotations

I have two

UIViews
in my
UIViewController
. I want one to rotate and the other one to be locked. Basically, one UIView can switch from Portrait and Landscape whereas the other is fixed in its "Portrait" mode.

At first I tried using
shouldAutorotate
but then I realized that that applies to everything that belongs to the
UIViewController
. Then I tried having two
UIViewController
s (which worked great for the rotation problem by the way) but I have
AVCaptureSession
going on in one view controller and I didn't want to have to connect the camera input and initialize the session every time.

Is there a way for me to be able to have one rotate while the other one is locked?

Thanks!

Answer

ALRIGHT it took me like a whole month and a half to figure it out but here is my solution:

It's pretty hard to keep accounting for all the separate UIViews rotations every time the device rotates so I went back to the two UIViewControllers approach.

This time around I decided to have a UINavigationController as the root view controller and I subclassed it. I set a NSNumber variable in my interface class to account for 2 or more view controllers a user could have. However, in my case, I would only really switch between 0 and 1. In addition, I overrode shouldAutoRotate and supportedInterfaceOrientations to have different properties according to the NSNumber variable.

Before explaining it any further, I'll post my code...

This is my interface class:

@interface MasterNavigationController : UINavigationController

@property (strong, nonatomic) NSNumber *num;

@end

And my implementation class:

#import "MasterNavigationController.h"

@implementation MasterNavigationController

- (BOOL)shouldAutorotate {
    if ([_num isEqualToNumber:[NSNumber numberWithInt:0]]) return NO;
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if ([_num isEqualToNumber:[NSNumber numberWithInt:0]]) return UIInterfaceOrientationMaskPortrait;
    return [[self topViewController] supportedInterfaceOrientations];;
}

@end

Whenever I was in my UIViewController that I didn't want to rotate, the NSNumber variable would be set to 0. Thus, the navigation controller would be able to tell if it should autorotate or not. Then when the navigation controller pushed the next view controller that can rotate, I set the NSNumber variable to 1 and the navigation controller was able to know it should be autorotating. This is great because whenever it pushed back to the non-rotating view controller it knew to reorient itself without getting all screwed up.

Hope this helps anyone else having trouble with this!!! It took me so long to figure this out.

Comments