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.
Written by Juwe
Related protips
4 Responses
I added galleryModalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; to center, else no move to x or y
@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;
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;
}
In IOS 8 Frame setting is not working , when changing the orientation. For the Presentation Style "Form Sheet".