Question

I have a UIView (A) which is beeing displayed in Portrait Mode and has a UIButton attached. When the user clicks the button a second UIView (B) is initialized and displayed as Modal View on top of UIView A.

Is it possible to configure the second Modal UIView to display in Landscape mode rather than Portrait mode by default? This rotation should work programmatically and independently to any real rotation of the iPhone/iPad device.

I tried to change the orientation with setStatusBarOrientation inside the method where the second UIView is allocated and displayed, but this does not work:

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

Heres the method for opening the modal view:

- (void) showModalView
{
    // get screen dimensions
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

    // init elements
    UIViewController *overlayVc = [[UIViewController alloc] init];
    UIView        *overlayView  = [[UIView alloc] initWithFrame:CGRectMake(0,0, screenWidth, screenHeight)];
    UIImageView   *bgImgView    = [[UIImageView alloc] initWithFrame:overlayView.bounds];

    // set status bar orientation
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

    // add overlay to view
    [overlayView addSubview:bgImgView];
    [overlayVc setView:overlayView];

    // show modal vc
    [self presentViewController:overlayVc animated:YES completion:nil];
}

This is what i mean:

foo

Thank you very much in advance

Was it helpful?

Solution

Yes. First you need to tell your app that you support landscape mode. You do this in your project file, under the general tab of your apps target.

Then in your view controller you need to implement the methods under the section "Configuring the View Rotation Settings". Specifically this would look like:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight
}

- (BOOL)shouldAutorotate
{
    return YES;
}

Notice the function supportedInterfaceOrientations that we return a mask.

When you present modally your view should automatically rotate. You might also need to implement those same methods in your previous view controller so that when you go back it knows how to orient itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top