Search code examples
objective-cipadmodalviewcontrolleruimodalpresentationstyle

iPad: UIModalPresentationFormSheet fails on landscape mode


We've the next problem in our iPad version.

I've a NavigationController inside a UITabBar. I want to show a Form with a look&feel similar than the e-Mail form.

I use the same code to show a model centered:

// View to be displayed in the modal
AdhocViewController *controller = [[AdhocViewController alloc] initWithNibName:@"AdhocViewController" bundle:[NSBundle mainBundle]];
controller.caller = self;

// The form will need a navigation bar to cancel or save the form
UINavigationController *modalViewNavController = [[UINavigationController alloc] 
                                               initWithRootViewController:controller];

// Configurate the modal presentation and transition
modalViewNavController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
modalViewNavController.modalPresentationStyle = UIModalPresentationFormSheet;

// Show the new view
[self presentModalViewController:modalViewNavController animated:YES];

This code works perfectly on portrait mode, but on landscape the view appears partially out of the screen ... and I didn't found yet the way to solve it.

I test some of the solutions I found here ...

And try to add the next lines after preset the model view to resize it, but doesn't work it out

controller.view.superview.frame = CGRectMake(0, 0, 600, 700);
controller.view.superview.center = self.view.center;

Any suggestion?

Thanks,

Ivan

References in StackOverflow:


Solution

  • Finally the code was the next:

    // Remove the modalTransitionStyle to enable the default behaviour and change to PageSheet
    modalViewNavController.modalPresentationStyle = UIModalPresentationPageSheet;
    // Present the modal view and adapt the center depending of the orientation
    [self presentModalViewController:modalViewNavController animated:YES];
    
    UIDeviceOrientation _orientation = [controller interfaceOrientation];
    if (UIDeviceOrientationIsPortrait(_orientation)){
        modalViewNavController.view.superview.center = CGPointMake(768/2, 1024/2 + 10);
    } else {
        modalViewNavController.view.superview.center = CGPointMake(768/2 - 10, 1024/2);
    }
    

    The +10 and -10 is because by deafult the NavigationController of the modal was out of the screen on top.

    It is ... a crappy solution :SS but works ... Although if anybody have suggestions would be nice to know.

    Seams that if I need to include the same center for both orientation, maybe the orientation of the superview is not the expected one.

    In this solution, when I dissmiss the modal view on Portrait orientation, at least on the iPad simulator, it rotate to portrait mode automatically ...

    The final solution was to execute the presentModalViewController over the main controller, the UITabBar, and update the dismiss method to be executed also over it.

    [tabbar presentModalViewController:modalViewNavController animated:YES];
    
    UIDeviceOrientation _orientation = [controller interfaceOrientation];
    if (UIDeviceOrientationIsPortrait(_orientation)){
        modalViewNavController.view.superview.center = CGPointMake(768/2, 1024/2 + 10);
    } else {
        modalViewNavController.view.superview.center = CGPointMake(1024/2, 768/2 + 10);
    }
    

    Finally!!!!

    Thank you,

    Iván