1

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 UIViewControllers (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!

yun
  • 1,243
  • 11
  • 30

2 Answers2

1

You can do it with shouldAutorotate i think. when device change orientation your both view will get rotate then manually rotate your fixed view with initial position by using transform something like below,

  myView.transform = CGAffineTransformMakeRotation(M_PI_2);

You can use delegate like below to perform your manual rotation

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  // do something before rotation
}

or

 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
 {
    // do something after rotation
 }

Update :

refer this answer for disable animation! Hope this will help :)

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • Is it possible to disable the animation for the rotation? Thanks I will try this out later today – yun May 13 '16 at 14:50
  • ACTUALLY just figured it out, I got one to rotate and the other to be fixed while `shouldAutorotate` is set to `NO`. However, now the rotated view stays in portrait (it's just a portrait rotated). How do I fix this? – yun May 14 '16 at 18:22
0

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.

yun
  • 1,243
  • 11
  • 30