4

I am using storyboards and I have a UINavigationController embedded in a UITabBarController. i push a view controller then from this view controller I present a MODAL UINavigationController with a UIViewController.

The problem is, the modal view controller can rotate when all my view previous to the modal view can't. How do I stop the Modal nav controller allowing any rotation?

I have tried adding:

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

and

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

Thanks

Darren
  • 10,182
  • 20
  • 95
  • 162
  • Have you tried editing the info.plist? Use Supported interface orientations. – Majster Nov 11 '12 at 17:51
  • I use other orientations for different views, I just need to restrict this view. – Darren Nov 11 '12 at 18:09
  • @Darren Here you should go for the same ...http://stackoverflow.com/questions/13023936/orientation-issue-in-ios-6/13024015#13024015. you may get any idea reagarding the same – Kamar Shad Nov 11 '12 at 18:38
  • I just tried changing the plist but then some of my other views crash – Darren Nov 11 '12 at 18:38
  • thanks iOS-Developer i've just tried that, but unfortunately the VC still rotates :( – Darren Nov 11 '12 at 18:43
  • I have a `TabBarViewController`subclass which I use `-(NSUInteger)supportedInterfaceOrientations { NSLog(@"%s",__FUNCTION__); return UIInterfaceOrientationMaskPortrait; }` that gets called with every view. It stops my other views rotating, but not after my Modal View! – Darren Nov 11 '12 at 19:04

1 Answers1

5

try to categories UINavigationController

@implementation UINavigationController (Rotation_IOS6)

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

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
Janub
  • 1,594
  • 14
  • 26
  • 4
    Very dangerous - a category will replace the initial implementation of a method and you don't know what other logic is applied in `UINavigationController`'s method. A better solution would be to create your own abstract subclass of `UIViewController` (eg called `MyAbstractViewController`) containing the methods in your answer and whenever you want a view, don't subclass `UIViewController`, subclass `MyAbstractViewController` instead. – deanWombourne Nov 11 '12 at 19:13
  • 3
    This worked. I created a UINavigationController subclass with these methods and used it as the class for the NavController that is after the modal view. Both these and the ones in the TabController are called, but these make it work. deanWombourne, not sure that would work as adding the methods to the VC's don't work. – Darren Nov 11 '12 at 19:15
  • @deanWombourne subclassing UIViewController doesn't work. Subclassing UINavigationController does though. I agree though, a category for this is dangerous and a subclass is the way to go. – Dex Dec 02 '12 at 03:41
  • if it have a model viewcontroler .....,so i recommend to use self.visibleViewController xxxx – ibcker Jul 17 '15 at 17:22