Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show modal view controller with custom frame in iPad

I want to show a modal UIViewController with a custom frame in iPad, centered on top of its parent view controller.

I tried using a form sheet but as far I know the frame and shadow effect can't be changed.

vc.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:cv animated:YES];

I also tried using a popover but as far as I know either I can't center it or I can't hide the arrow.

Is there another way to show modal view controllers? Is it possible to solve this problem by using form sheets or popovers?

like image 846
hpique Avatar asked Nov 01 '10 00:11

hpique


1 Answers

I'm using the following method to create a modal view controller with any given size centered in the screen. You may change it to center in the parent view controller if you want. This is working from storyboard so you maybe can send the view controller as an argument instead. But apart from that it does what you need.

NOTE: I've found that if you try to show another modal over this one it will return to the original size.

- (UIViewController *) presentModalViewControllerWithIdentifier:(NSString *)identifier 
                                                        andSize:(CGSize)size 
                                        andModalTransitionStyle:(UIModalTransitionStyle)modalTransitionStyle {
    UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];


    viewController.modalPresentationStyle = UIModalPresentationPageSheet;
    viewController.modalTransitionStyle = modalTransitionStyle;
    [self presentModalViewController:viewController animated:YES];
    viewController.view.superview.autoresizingMask = 
    UIViewAutoresizingFlexibleTopMargin | 
    UIViewAutoresizingFlexibleBottomMargin | 
    UIViewAutoresizingFlexibleLeftMargin | 
    UIViewAutoresizingFlexibleRightMargin;    
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    viewController.view.superview.frame = CGRectMake(0, 0, size.width, size.height);
    CGPoint center = CGPointMake(CGRectGetMidX(screenBounds), CGRectGetMidY(screenBounds));
    viewController.view.superview.center = UIDeviceOrientationIsPortrait(self.interfaceOrientation) ? center : CGPointMake(center.y, center.x);

    return viewController;
}

If it's the only thing you want it works fine. But what I've found is that Apple's implementation of modal view controllers is a little counter productive as you cannot access any of it's underlying parts and so you cannot fully animate them. Useful if you want to have your own transitions.

And it doesn't fall also in the category of a child view controller as it stands over all the other views in the window.

like image 92
Fábio Oliveira Avatar answered Sep 20 '22 02:09

Fábio Oliveira