Last Updated: February 25, 2016
·
18.56K
· juwe

Resizing a view presented modally (iPad)

If you have tried this in the past maybe you finished in a dead end, but it's easier than yo thought.

This is the code to present a view controller modally:

UIViewController *myLOLViewController = [[UIViewController alloc] init]
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myLOLViewController];
[navigationController setModalPresentationStyle:UIModalPresentationFormSheet];

[self presentModalViewController:navigationController animated:YES];

The logic says that we have to try to resize the Modal before it's presented, so, i tried a lot of ways to do it but no one worked.

The trick is to resize the modal view "after" is presented, this is the full code:

UIViewController *myLOLViewController = [[UIViewController alloc] init]
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myLOLViewController];
[navigationController setModalPresentationStyle:UIModalPresentationFormSheet];

[self presentModalViewController:navigationController animated:YES];

navigationController.view.superview.frame = CGRectMake(0, 0, 600, 100);
navigationController.view.superview.center = self.view.center;

After all you just need to setCenter because it has changed after the resize. Obviously this works just for iPad; in iPhone the modally way to present views is a little bit different because of the space.

4 Responses
Add your response

I added galleryModalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; to center, else no move to x or y

over 1 year ago ·

@paulomcnally, nice job! You found an excellent solution for iOS7. Complete code here for those who are interested.

UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:navigationController animated:YES completion:nil];
navigationController.view.superview.frame = CGRectMake(0, 0, 800, 544);
navigationController.view.superview.center = self.view.center;

over 1 year ago ·

This doesn't seem to be working for me. I am doing the following, but the size is completely ignored. Does it have to do with the fact that I am loading my view from a nib?

  • (void)swipedUp:(id)sender {
    NSLog(@"Swiped up");
    paletteViewController *viewController = [[paletteViewController alloc] initWithNibName:@"paletteViewController" bundle:nil];

    viewController.delegate = self;
    [ viewController updateSelectedColor:self.drawView.selectedColor];
    [ viewController setSize:(ushort)self.drawView.selectedSize];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
    navigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:navigationController animated:YES completion:nil];
    navigationController.view.superview.frame = CGRectMake(0, 0, 480, 280);
    navigationController.view.superview.center = self.view.center;
    }

over 1 year ago ·

In IOS 8 Frame setting is not working , when changing the orientation. For the Presentation Style "Form Sheet".

over 1 year ago ·