nabrugir
nabrugir

Reputation: 1869

Force UIViewController full screen and landscape

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.

Upvotes: 0

Views: 1463

Answers (2)

Léo Natan
Léo Natan

Reputation: 57060

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:

Upvotes: 0

MCMatan
MCMatan

Reputation: 8863

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!

Upvotes: 0

Related Questions