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

3 Answers 3

2

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

- (BOOL)shouldAutorotate {
   return NO;
}
0

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES/NO;
}
11
  • 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 9, 2014 at 1:34
  • Please refer this url once: stackoverflow.com/questions/14174449/…
    – Ashutosh
    Jan 9, 2014 at 5: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 9, 2014 at 6:03
  • Please try this method (stackoverflow.com/questions/12630359/… (BOOL)shouldAutorotate { return YES; }
    – Ashutosh
    Jan 9, 2014 at 6: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 9, 2014 at 7:20
0

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

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 stackoverflow.com/help/how-to-answer
    – wmk
    Aug 17, 2015 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.