0

How can I force one of my viewcontrollers to be full screen and landscape in iOS? My app is in portrait mode but I want that only one of the views is landscape and fullscreen.

I tried this solution which consists off implementing CustomUINavigationController with the following method and adding the shouldAutorotate methods in my viewcontroller I want to rotate:

//Custom UINAvigationController

-(NSUInteger)supportedInterfaceOrientations{

     return UIInterfaceOrientationMaskLandscapeRight;

   }
  - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
     return UIInterfaceOrientationLandscapeRight;
   }
 - (BOOL)shouldAutorotate{
      return YES;
   }

//UIViewController I want to automatically rotate

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

I guess my problem it is related with the view's herachy. It works if my ViewController is my initial view controller but not with all the views. I have a custom menu and a placeholder (view) where I display all the other views. The viewcontroller I want to be landscape and fullscreen comes from a view which goes inside the placeholder, maybe this is the problem but I don't now how to solve it.

Community
  • 1
  • 1
nabrugir
  • 1,839
  • 2
  • 22
  • 38
  • A duplicate of [How to handle different orientations in iOS 6](http://stackoverflow.com/questions/15947349/how-to-handle-different-orientations-in-ios-6). See my answer there for a project example of exactly what you need. – Léo Natan Mar 06 '14 at 22:42
  • @LeoNatan It is not working. It works in a separate project but not in my project with my views herachy. I receive the warning: Presenting view controllers on detached view controllers is discouraged. I have a custom menu and a placeholder (view) where I see all the other views. The viewcontroller I want to be landscape and fullscreen comes from a view which goes in the placeholder, maybe this is the problem – nabrugir Mar 09 '14 at 15:27
  • 1
    That warning is thrown when you have a view controller that does not have a parent or presenting view controller. You should remember to add all child view controllers as children to the presenting view controller. – Léo Natan Mar 09 '14 at 21:08
  • See my answer for a better solution! – Léo Natan Mar 10 '14 at 12:43
  • 1
    The problem was related with the child view controllers. After adding all child view controllers as a children to the view controller it worked. Thank you @LeoNatan – nabrugir Mar 10 '14 at 15:26

2 Answers2

0

So when a viewController is called to present is self, the following methods will call:

 -(NSUInteger)supportedInterfaceOrientations{

     return UIInterfaceOrientationMaskLandscapeRight;

   }
  - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
     return UIInterfaceOrientationLandscapeRight;
   }
 - (BOOL)shouldAutorotate{
      return YES;
   }

And as you can see, by placing them i'm forcing the UIViewContoller to display LandscapeRight.

Important not! : if you use UINavigation controller, you need to create some "master" navigation controller subview, and call these methods there.

Hope this helps!

MCMatan
  • 8,623
  • 6
  • 46
  • 85
  • I get the error Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'. I guess it is because I am not supporting landscape mode (my views do not rotate), do you know how I can solve? – nabrugir Mar 06 '14 at 23:42
  • Try to click on your app, and then "General", and check if the "Lanscape Left" and "Landscape Right" are checked. – MCMatan Mar 06 '14 at 23:57
  • It is not working. I receive the warning: Presenting view controllers on detached view controllers is discouraged. I have a custom menu and a placeholder (view) where I see all the other views. The viewcontroller I want to be landscape and fullscreen comes from a view which goes in the placeholder, maybe this is the problem. – nabrugir Mar 09 '14 at 15:19
0

You can use my LNWindowManager to present a view controller modally in a different window.

https://github.com/LeoNatan/LNWindowManager

It exposes a similar API to modal view controller presentation, but is extended to windows:

- (IBAction)displayButtonTapped:(UIButton *)sender
{
    UINavigationController* nvc = [self.storyboard instantiateViewControllerWithIdentifier:@"__presentationNVC"];
    nvc.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissWindow:)];

    UIWindow* window = [LNWindowManager templateWindowForName:@"example"];
    window.rootViewController = nvc;

    [[LNWindowManager sharedWindowManager].topWindow presentWindow:window animated:YES completion:nil];
}

- (void)dismissWindow:(UIBarButtonItem*)barButtonItem
{
    [[LNWindowManager sharedWindowManager].topWindow.presentingWindow dismissWindow:[LNWindowManager sharedWindowManager].topWindow animated:YES completion:nil];
}

You can then determine the orientation for each window in with the AppDelegate method:

– application:supportedInterfaceOrientationsForWindow:
Léo Natan
  • 56,823
  • 9
  • 150
  • 195