How to resize UIModalPresentationFormSheet?

I am trying to display a modal presentation controller as a UIPresentationFormSheet. A view will appear, but I cannot resize it. My XIB has the correct height and width, but it seems to be overridden when I call it this way:

composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil]; composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:composeTweetController animated:TRUE]; 

Any thoughts? I am using iPhone SDK 3.2 beta 4

+43
objective-c iphone cocoa-touch uikit
Mar 16 '10 at 20:39
source share
12 answers

You can customize the frame of the modal view after its presentation:

Tested in iOS 5.1 - 7.1

 MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease]; targetController.modalPresentationStyle = UIModalPresentationFormSheet; targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; //transition shouldn't matter [self presentViewController:targetController animated:YES completion:nil]; if(floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1){ targetController.view.superview.frame = CGRectInset(targetController.view.superview.frame, 100, 50); }else{ targetController.view.frame = CGRectInset(targetController.view.frame, 100, 50); targetController.view.superview.backgroundColor = [UIColor clearColor]; } 
+90
Nov 24 '10 at 20:42
source share

Here we use a method that works on iOS7 , as well as iOS5 and iOS6: (arc)

 -(void)presentController:(UIViewController*)controller fromRootController:(UIViewController*)rootController withSize:(CGSize)size { UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:controller]; nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical; nav.modalPresentationStyle = UIModalPresentationFormSheet; [rootController presentModalViewController:nav animated:YES]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { nav.view.superview.backgroundColor = [UIColor clearColor]; nav.view.bounds = CGRectMake(0, 0, size.width, size.height); } else { nav.view.superview.bounds = CGRectMake(0, 0, size.width, size.height); } } 
+13
Dec 10 '13 at 17:49
source share

 composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil]; composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:composeTweetController animated:TRUE]; //if you want to change its size but the view will remain centerd on the screen in both portrait and landscape then: composeTweetViewController.view.superview.bounds = CGRectMake(0, 0, 320, 480); //or if you want to change it position also, then: composeTweetViewController.view.superview.frame = CGRectMake(0, 0, 320, 480); 
+12
Feb 17 '11 at 16:30
source share

Starting with iOS7 you can just do

 composeTweetController.preferredContentSize = CGSizeMake(380.0, 550.0); 
+5
Apr 01 '15 at 10:36
source share

Tested and works for iOS 6 using Xcode 4.5

I came across my answer after reading a lot of tips here:

  • In the viewWillAppear:(BOOL)animated method viewWillAppear:(BOOL)animated :

     [super viewWillAppear:animated]; //resize modal view self.view.superview.bounds = CGRectMake(0, 0, 432, 680); 
  • In the viewDidAppear:(BOOL)animated method:

     CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2); self.view.superview.center = centerPoint; 

I tried to add the centering code in the same place as the resize code, but that didn't work. For some reason, it only works after the view has appeared on the screen.

I guess this has something to do with how the UIModalPresentationFormSheet works, because when I went through the LLDB debugger, I noticed that there is a _formSheetSize variable that is still {540, 620}. Hover over your mouse.

+4
Oct 02
source share

This will work with any UIModalTransitionStyle

 @interface BaseDialog () @property(nonatomic,assign) CGRect origFrame; @end @implementation BaseDialog -(void)viewDidLoad{ [super viewDidLoad]; self.origFrame=self.view.frame; } -(CGSize)formSheetSize{ return self.origFrame.size; } 
+4
Oct 08 '13 at 11:10
source share

The next first code is the model view manager view

  composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil]; composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:composeTweetController]; navigationController.delegate = self; [self presentModalViewController:navigationController animated:YES]; 

And in the ComposeTweet controller class

 -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.view.superview.frame = CGRectMake(0, 0,500,400); } 
+4
Oct 09 '13 at 8:57
source share

iOS 7

So, the approach of setting the framework or boundaries of the supervisor no longer works on iOS 7 (for UIModalTransitionStyleCoverVertical). However, you can now set the background color of the supervisor to clear it, and then change the frame of the modal view controller as you see fit.

So, in iOS 7 you need to:

  • represents a presentation controller (presentation mode: UIModalPresentationFormSheet)
  • set the backlight color of the supervisor controller for cleaning.
  • change the controller’s viewing frame as desired (possibly by reducing it and then centering it in the viewing mode)
+3
Sep 21 '13 at 18:57
source share

I got a full-screen modal view from a UIPresentationFormSheet with just this line:

 modalViewController.view.superview.bounds = CGRectMake(0, 0, 1024, 748); 
+2
Jun 20 2018-12-12T00:
source share

@bdrobert, unfortunately, your pleasant solution no longer works on iOS6 - the container retains its original size, even if the new view controller is built-in with a non-standard size.

You probably need to use the containment API introduced in iOS5 , but you need to add your own dull background, retrieving all touch events for this area.

+2
Sep 24
source share

When changing the orientation, this code works fine.

settingViewController.modalPresentationStyle = UIModalPresentationFormSheet;

  [self presentViewController:settingViewController animated:YES completion:nil]; settingViewController.view.superview.frame = CGRectMake(0, 0, 700, 700); UIInterfaceOrientation currentOrientation = [self getDeviceOrientation]; if(currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown) { CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2); settingViewController.view.superview.center = centerPoint; } else { CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.height/2, [[UIScreen mainScreen] bounds].size.width/2); settingViewController.view.superview.center = centerPoint; } 
0
May 01 '13 at 12:24
source share

The size of the view is fixed. You will need to implement things yourself if you want something else.

-four
Apr 24 '10 at 17:44
source share



All Articles