1

I have two view controllers embedded in a navigation controller, My first view controller is allowed to rotate whereas my second view controller should always open in portrait mode, For instance even if I am in landscape mode in first view controller I want to open my second in portrait only,

I present the second view controller by pushing segue from first.

Avinash Sharma
  • 665
  • 1
  • 7
  • 23
  • Possible duplicate of [how to force view controller to stay in portrait mode?](http://stackoverflow.com/questions/16720968/how-to-force-view-controller-to-stay-in-portrait-mode) – Ronak Chaniyara Apr 13 '16 at 09:04
  • Possible duplicate of [How to force a UIViewController to Portait orientation in iOS 6](http://stackoverflow.com/questions/12520030/how-to-force-a-uiviewcontroller-to-portait-orientation-in-ios-6) – kb920 Apr 13 '16 at 09:18

4 Answers4

2

You will have to implement shouldAutorotate on the second VC

Just before you present the second VC (portrait) call

 if([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
 }
Sean Lintern
  • 3,103
  • 22
  • 28
1

You can use this method in ur AppDelegate Class for and maintain with bool var

-(NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if(self.isDisplayInPoratrait){ 
        return UIInterfaceOrientationMaskPortrait;
    else
        // return ur required orientation
}

self.isDisplayInPoratrait is bool variable declared in AppDelagate set this variable yes in class which u want to present in portrait. Place this method in class which u wan to present in portrait

-(BOOL)shouldAutorotate {
   return NO;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
   return  UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {  
   return UIInterfaceOrientationLandscapeLeft;
}
Mahendra
  • 8,448
  • 3
  • 33
  • 56
Hardik Baldha
  • 159
  • 1
  • 14
0

Solution 1 : create a custom UINavigationController subclass and override these methods like this :

@interface LockedNavigationViewController ()

@end

@implementation LockedNavigationViewController

-(BOOL)shouldAutorotate {
    return UIInterfaceOrientationMaskPortrait;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

@end
Dinesh
  • 1,137
  • 9
  • 14
0

enter image description here

Select your project - > general, then follow the above image.

I'm a Learner
  • 193
  • 1
  • 2
  • 15