6

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
n00bAppDev
  • 610
  • 3
  • 10
  • 21

3 Answers3

2

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

- (BOOL)shouldAutorotate {
   return NO;
}
DustinB
  • 11,037
  • 5
  • 46
  • 54
0

In each viewcontroller you can write following code and return yes or no as per requirement.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES/NO;
}
Ashutosh
  • 2,215
  • 14
  • 27
  • Maybe I am not implementing it right, but what I did is I simply copied your code into a UIViewController class that I created which references to a view controller, and I returned 'NO'. This DID NOT lock the orientation of that view controller, I was still able to rotate the devices orientation. I then tried returning 'YES', and that too did not do anything at all. Any Ideas? – n00bAppDev Jan 09 '14 at 01:34
  • Please refer this url once: http://stackoverflow.com/questions/14174449/lock-the-orientation-of-viewcontroller – Ashutosh Jan 09 '14 at 05:02
  • Could you please explain how I should use the code in the answer provided on that thread? As I copied it into my tab bar controller and nothing happened, and when copied into a view controller it returned errors. – n00bAppDev Jan 09 '14 at 06:03
  • Please try this method (http://stackoverflow.com/questions/12630359/ios-6-how-do-i-restrict-some-views-to-portrait-and-allow-others-to-rotate):- (BOOL)shouldAutorotate { return YES; } – Ashutosh Jan 09 '14 at 06:17
  • Is there anyway you could please be a bit more specific? I have tried the code on that page but it doesn't work, I really feel as though I am not implementing it correctly. If you could tell me where to put what code, that would be greatly appreciated. Remember, the view controllers that I want to allow to rotate are under a navigation controller which is under a tab bar controller, which I feel is a big part of my problem. Thanks heaps – n00bAppDev Jan 09 '14 at 07:20
  • If you can provide your code or any sample app with replicated code then I can check that. – Ashutosh Jan 09 '14 at 07:22
  • I'll create a basic application that simulates my problem, and Ill copy the code over for each class. I'll append it to my question, so check back in like 5 mins. Thanks again. – n00bAppDev Jan 09 '14 at 07:31
  • There are more view controllers that I would like to allow to rotate, and more views that I would lock in portrait although I didn't want the question to get too lengthy. Feel free to ask questions, or if you want me to upload my application so you can view it better. Thanks! – n00bAppDev Jan 09 '14 at 07:44
  • I have the same situation here, did you find a solution? – David Liu Feb 20 '14 at 13:56
  • -(BOOL)shouldAutorotate {return NO;} on all the classes you would like NOT to rotate. Works for me. – Milo Mar 05 '14 at 01:28
  • `-(BOOL)shouldAutorotate {return NO;} ` works if the controller is outsides other controller. But in my and @n00bAppDev case, we have our controller inside a `UITabBarController`, and what i could understand, when the tab bar controller rotate, forces all the controllers inside him to rotate. So i have been forced to implement rotation in all the controller inside my tab bar. – IgniteCoders Apr 08 '14 at 12:58
0

Very good description of fixing this is at http://koreyhinton.com/blog/lock-screen-rotation-in-ios8.html

Kamal
  • 1
  • 2
    Welcome to Stackoverflow. Please don't just paste links as answers, as links might change over the time, please provide the answer itself or at least more context to the link. See http://stackoverflow.com/help/how-to-answer – wmk Aug 17 '15 at 19:33