1

I have posted a question here . In which one buddy replied and gave solution. Although the solution is working on few view controllers but on view its not working. When I enter a view controller that has TabController + navigation controller on there tab Bar Items, the code doesn't work. and the views are able to rotate.

I used the Following code for iOS 6.1

//For iOS6
- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations {
   return UIInterfaceOrientationMaskPortrait;
}

I have to implement the same thing in iOS 7 too. Please Help

Community
  • 1
  • 1
Jasmeet
  • 1,522
  • 2
  • 22
  • 41
  • 2
    What do you want? Stop rotation for all viewcontroller or stop rotation for a few viewcontroller? – Rashad Apr 20 '14 at 07:25
  • @Rashad I want to disable rotation for all except one where I have to show MPMoviePlayerController in both landscape and portrait mode – Jasmeet Apr 20 '14 at 09:23

3 Answers3

2
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation

The above methods don't get called of a viewcontroller if they are inside any tabbarcontroller of navigationcontroller. If these methods are declared inside tabbarcontroller or navigation controller then they will get called.

For solving this make a class FixedOrientationTab, it is a subclass of UITabBarController, and a navigation class OrientationEnabledNavigation, it is a subclass of UINavigationController. Then implemented shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation methods inside FixedOrientationTab and OrientationEnabledNavigation.

OrientationEnabledNavigation.h

#import <UIKit/UIKit.h>

@interface OrientationEnabledNavigation : UINavigationController

@end

OrientationEnabledNavigation.m

#import "OrientationEnabledNavigation.h"

@interface OrientationEnabledNavigation ()

@end

@implementation OrientationEnabledNavigation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
//    return NO;
}

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

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

FixedOrientationTab.h

#import <UIKit/UIKit.h>

@interface FixedOrientationTab : UITabBarController

@end

FixedOrientationTab.m

#import "FixedOrientationTab.h"

@interface FixedOrientationTab ()

@end

@implementation FixedOrientationTab

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
    //    return NO;
}

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

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


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Then if you want to use navigation controller in your project then use OrientationEnabledNavigation and for tabbar FixedOrientationTab. After that if you implement shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation these method inside your viewcontroller then they will get called.

Hope this helps.. :)

Edit As Jeev pointed out another way is that add some code beginning of your app delegate:

For UITabBarController

@implementation UITabBarController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
    if ([self.selectedViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.selectedViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    else {
        return UIInterfaceOrientationMaskAll;
    }
}

@end

For UINavigationController

@implementation UINavigationController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
    if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.topViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

-(NSUInteger) supportedInterfaceOrientations {
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

@end
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • thanks a lot for detailed explanation. Let me check, I will get back in few minutes,,,Thanks again – Jasmeet Apr 20 '14 at 09:48
  • I hv been in dvlpmnt from last 6 mnths,But I could'nt properly implement the method u mentioned above. Moreover I read some where that apple discards the idea of subclassing the UitabBarController.Then I moved.Then I found a soln of making category over http://www.kurutepe.com/2013/02/how-to-your-uitabbarcontrollers-and-uinavigationcontrollers-respect-your-auto-rotation-choices/ It worked perfectly for both iOS 6 and 7. The solution is very much similar to yours one. But I didn't create a Different UIatbarclass in my project, just made it inside the AppDelegate class...thanks a ton – Jasmeet Apr 21 '14 at 10:03
  • 1
    Thanks. Thats why I love SO. Learned a new thing.. I am also gonna add this to my answer. It'll help others. :) – Rashad Apr 21 '14 at 10:20
0

I have UINavigationController contains UIViewController.

You should disable rotate in UINavigationController by subclass UINavigationController and implement

- (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
// return some
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
// return some thing that you want
}

Hope this helps you.

nmh
  • 2,497
  • 1
  • 15
  • 27
0

Since I m using a TabBarController, the only working solution I got was to create a category.

so just make a category

here's the .h class of the category, I named it AutoRotationtab

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface UITabBarController (AutoRotationTab)

@end

here's the .m class of the category

#import "UITabBarController+AutoRotationTab.h"

@implementation UITabBarController (AutoRotationTab)

-(BOOL)shouldAutorotate
{
if(globalvariable_shouldRotate)
{
    return YES;
}
else
{
    return NO;
}
}


- (NSUInteger)supportedInterfaceOrientations
{

if(globalvariable_shouldRotate)
{
    return UIInterfaceOrientationMaskAll;
}
else
{
    return UIInterfaceOrientationMaskPortrait;
}
}

just make this category, and see the Magic, It works

Jasmeet
  • 1,522
  • 2
  • 22
  • 41