UIModalPresentationFormSheet resizing view

2019-01-16 06:23发布

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.

6条回答
迷人小祖宗
2楼-- · 2019-01-16 07:07

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

查看更多
Juvenile、少年°
3楼-- · 2019-01-16 07:10

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
查看更多
倾城 Initia
4楼-- · 2019-01-16 07:15
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);
查看更多
虎瘦雄心在
5楼-- · 2019-01-16 07:18

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.

查看更多
倾城 Initia
6楼-- · 2019-01-16 07:22

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.

查看更多
Anthone
7楼-- · 2019-01-16 07:23

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.

查看更多
登录 后发表回答