Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 - Lock Orientation of only SOME View Controllers that are within a Tab Bar?

I know this question has been asked a countless number of times, although I have not been able to get it to work at all, literally no answer has worked, maybe I'm not copying the code across correctly, but either way it wont work.

Anyway, my application's view controller layout is as follows, note that this is a Tabbed Application, NOT a single View Application:

Master Tab Bar Controller > Branches off into multiple Navigation Bar Controllers (all referencing to the same CustomNavigationController class) > Under each Nav Controller are a number of UIViewControllers, some of which are referencing custom view controller classes. (see pic for a visual representation of the hierarchy) View Controller Layout

My question is: how can I lock the orientation of all pages in my application to portrait mode ONLY, however allow some pages (UIViewControllers) to be able to rotate to landscape if the user rotates the device? (The pages marked with a star, in the image above are the ones I would like to ALLOW rotation for)

Remember the view controllers that I want to allow rotation for are children to a Navigation controller which is also a child to a Tab Bar Controller. Also, note that one of the View controllers isn't under a Navigation controller, it is just under the Tab Bar controller, not sure how much this affects though.

Thank you so much

Code from the relevant classes of my app

Tab Bar Controller Code (note there is not meaningful code in here as I never used to have a class for this)

@interface TabBarController ()

@end

@implementation TabBarController

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

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

Navigation Controller Class (Also never had a class for this

@interface NavigationBar ()
@end   
@implementation NavigationBar

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"NavigationController";
    }
    return self;
}

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

@end

Home Page Class (Must be locked in portrait)

@interface HomePage ()

@end

@implementation HomePage


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"HomePage";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // If the condition is not true/not YES, than create and show alert.
    if (![[NSUserDefaults standardUserDefaults]
          boolForKey:@"didShowOneTimeAlert"]) {

        //Define and Edit the Alert details.
        UIAlertView *zoomTip = [[UIAlertView alloc] initWithTitle:@"Tips" message:@"On all web site and map pages, pinch in and out to zoom. \n \n On the 'Program' page, tap on a session to view more details." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];

        //Show the alert
        [zoomTip show];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"didShowOneTimeAlert"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } // End if
}

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

Map Class (A page that displays a map and MUST be able to rotate)

@interface MezzanineLevelMap ()
@end
@implementation MezzanineLevelMap
@synthesize scrollView, imageView;


-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Mezzanine Level"]];
    self.imageView = tempImageView;

    scrollView.maximumZoomScale = 1;
    scrollView.minimumZoomScale = .25;
    scrollView.clipsToBounds = YES;
    scrollView.delegate = self;
    [scrollView addSubview:imageView];
    scrollView.zoomScale = .25;

    // Change scroll view sizes according to the screen size
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

        CGSize result = [[UIScreen mainScreen] bounds].size;
        if (result.height == 480) {
            [scrollView setFrame:CGRectMake(0, 5, 320, 475)];
        } else {
            [scrollView setFrame:CGRectMake(0, 5, 320, 563)];
        } // End if
    } // End if
}

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

@end
like image 639
n00bAppDev Avatar asked Jan 08 '14 08:01

n00bAppDev


1 Answers

Just implement the following in your view controller (iOS 6+):

- (BOOL)shouldAutorotate {
   return NO;
}
like image 105
DustinB Avatar answered Oct 22 '22 03:10

DustinB