- 1 year ago 70
iOS Question

Two Separate UIView Rotations

I have two

in my
. 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
but then I realized that that applies to everything that belongs to the
. Then I tried having two
s (which worked great for the rotation problem by the way) but I have
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?


Answer Source

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;


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];;


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.

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