iOS: Forcing orientation in your view controller (iOS 6 and iOS < 6)

Got this trouble on forcing the app to be in a a landscape mode. Let say, you have a standard orientation of a Portrait but at first force the view controller to be in landscape where the button is located in the left side.

So I have this code that you can try, play, and modify. See below,


  1. //
  2. //  MainViewController.h
  3. @interface MainViewController : UIViewController <UIWebViewDelegate>
  4. @end
  5. // MainViewController.m
  6. #import "MainViewController.h"
  7. @implementation UINavigationController (autorotation)
  8. // >= iOS 6
  9. -(BOOL)shouldAutorotate
  10. {
  11.    
  12.     UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
  13.     NSLog(@"intefaceorientation: %d", interfaceOrientation);
  14.     [self.topViewController shouldAutorotate];
  15.     return YES;
  16.    
  17. }
  18. // >= iOS 6
  19. -(NSUInteger)supportedInterfaceOrientations
  20. {
  21.     return UIInterfaceOrientationMaskLandscapeLeft;
  22.    
  23. }
  24. @end
  25. @implementation MainViewController    
  26.    
  27. #pragma mark view did load
  28. - (void)viewDidLoad
  29. {
  30.    
  31.     [super viewDidLoad];
  32.    
  33.     self.navigationController.navigationBarHidden = YES;
  34.    
  35. }
  36. - (void)viewDidAppear:(BOOL)animated
  37. {
  38. // < iOS 6
  39.     [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
  40. }
  41. // < iOS 6
  42. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  43. {
  44.     if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
  45.         return YES;
  46.     }
  47.    
  48.     return NO;
  49. }
  50. - (void)viewDidUnload {
  51.    
  52.     [super viewDidUnload];
  53. }
  54. @end


or


Comments

Popular posts from this blog

Converting sectors into MB - Useful in understanding the sectors in iostat in Linux

What is Disk Contention?

Installing MySQL from source: Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)