43

I am interested to know on how I can resize the view when using UIModalPresentationFormSheet of modalPresentationStyle, it looks like it has a fixed size so I was wondering if anyone out there did managed to manipulate the popup view from the sizing perspective.

So there is either UIModalPresentationFormSheet with a fixed view size or full views and I am after something in between.

Bartosz Ciechanowski
  • 10,293
  • 5
  • 45
  • 60
tosi
  • 1,873
  • 2
  • 18
  • 38
  • 2
    possible duplicate of [How to resize a UIPresentationFormSheet?](http://stackoverflow.com/questions/2457947/how-to-resize-a-uipresentationformsheet) – JosephH Mar 02 '12 at 18:04

6 Answers6

71
MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease]; 

targetController.modalPresentationStyle = UIModalPresentationFormSheet;

targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[self presentModalViewController:targetController animated:YES];

// it is important to do this after presentModalViewController:animated:
targetController.view.superview.bounds = CGRectMake(0, 0, 200, 200);
Robert
  • 37,670
  • 37
  • 171
  • 213
tosi
  • 1,873
  • 2
  • 18
  • 38
  • 48
    This is not a good solution because it adds some kind of a blur to navigationItem title. This one is better: targetController.view.superview.bounds = CGRectMake(0, 0, 200, 200); It is still center, no need to do ...superview.center. – Borut Tomazin Jul 15 '11 at 08:27
  • The blurring is caused by subpixel placement. My answer above corrects this. targetController.view.superview.center = GPointMake(roundf(self.view.center.x), roundf(self.view.center.y)); – Brody Robertson Jun 05 '13 at 14:45
  • 1
    @BorutTomazin - Due to the amount of up-votes on your comment - I have edited the answer. – Robert Aug 21 '13 at 11:58
  • If you're having trouble with iOS 7, try doing the resizing/repositioning in the completion block of presentViewController:animated:completion and/or the presented View Controller's viewDidAppear (they have slightly different behaviors) – Rembrandt Q. Einstein Dec 11 '13 at 22:27
  • 11
    iOS 8: This stopped working in iOS 8 and no combination of the previous answers appeared to work. The solution, for me, was to remove all the lines referring to the superview and to add the following line before the "presentModalViewController" line. [code] targetController.preferredContentSize = CGSizeMake(200, 200);[/code] – Vince O'Sullivan Sep 18 '14 at 11:52
4

In ios8 and earlier works:

AboutViewController * _aboutViewController = [[AboutViewController alloc] init];
    _aboutViewController.modalPresentationStyle = UIModalPresentationFormSheet;
    if(IS_IOS8)
    {
        _aboutViewController.preferredContentSize = CGSizeMake(300, 300);
    }
    [self presentViewController:_aboutViewController animated:YES completion:nil];

In AboutViewController.m

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];

    if(!IS_IOS8)
    {
        self.view.superview.bounds = CGRectMake(0, 0, 300, 300);
    }
}

IS_IOS8

#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)

In iOS 8 you can also use UIPresentationController which gives you more customization options.

pawel_d
  • 487
  • 5
  • 9
4

You are able to adjust the frame of a modal view after presenting it:

Tested in iOS 5.1 - 6.1, using XCode 4.62

MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease];
targetController.modalPresentationStyle = UIModalPresentationFormSheet;
targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;  //transition shouldn't matter
[self presentModalViewController:targetController animated:YES];
targetController.view.superview.frame = CGRectMake(0, 0, 200, 200);//it's important to do this after presentModalViewController
targetController.view.superview.center = GPointMake(roundf(self.view.center.x), roundf(self.view.center.y));//self.view assumes the base view is doing the launching, if not you might need self.view.superview.center etc.

Update The preferred iOS 6.0 view controller presentation method also works correctly:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
Brody Robertson
  • 8,506
  • 2
  • 47
  • 42
  • thanks! MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease]; targetController.modalPresentationStyle = UIModalPresentationFormSheet; targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; //transition shouldn't matter [self presentModalViewController:targetController animated:YES]; targetController.view.superview.frame = CGRectMake(0, 0, 200, 200);//it's important to do this after presentModalViewController targetController.view.superview.center = self.view.center; – tosi Nov 26 '10 at 10:49
3

For iOS 8, simply implement the delegate method (CGSize)preferredContentSize on each view controller. It should resolve all the size issue.

rwang
  • 71
  • 5
2

Just to extent Fatos solution (great one) If your are creating the view controller using a .xib file after the alloc initWithNibName you could store the view frame:

CGRect myFrame = targetController.view.frame;
...
targetController.view.superview.bounds = myFrame;

And then use it for superview.bounds, so the view's size in the .xib will be used and you could change the size more visually.

LightMan
  • 3,517
  • 31
  • 31
-1

I put this code in the form sheet view controller:

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.view.superview.bounds = CGRectMake(0, 0, 540, 500);  // your size here
}

Note that the resizing occurs early enough that the the presentation animation looks correct.

Brian Shriver
  • 267
  • 1
  • 4