0

My app supports all interface orientations, but I want a certain view to be shown only in portrait. I have this implementation of an UINavigationController subclass:

@implementation CustomNavigationController

- (BOOL)shouldAutorotate
{
   return [[self.viewControllers lastObject] shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
   return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

// More methods

@end

Then, for its root view controller, I have implemented:

- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];

   NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
   [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return UIInterfaceOrientationPortrait;
}

This prevents the view of the view controller from being rotated to landscape when it is in portrait, but when I navigate to this view being the device already in landscape, and I have been previously in a view which do rotates to landscape, I see this view in landscape as well.

How could I solve this scenario?

AppsDev
  • 12,319
  • 23
  • 93
  • 186
  • try put your `viewWillAppear` code to `viewDidAppear`...maybe it can fix? – Tj3n Dec 11 '15 at 08:33
  • @Tj3n I've just tried, but I get the same result... – AppsDev Dec 11 '15 at 08:57
  • maybe ur vc is embed in tab bar or nav bar? check out [this link](http://stackoverflow.com/questions/14633213/force-portrait-orientation-while-pushing-from-landscape-view-controller) – Tj3n Dec 11 '15 at 09:06

0 Answers0