43

UIModalPresentationFormSheet我很想知道在使用of时如何调整视图的大小modalPresentationStyle,它看起来有一个固定的大小,所以我想知道是否有人确实设法从大小调整的角度来操纵弹出视图。

所以要么UIModalPresentationFormSheet有固定的视图大小,要么有完整的视图,我在两者之间寻找一些东西。

4

6 回答 6

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);
于 2011-01-31T12:18:02.683 回答
4

在 ios8 及更早的作品中:

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

在 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)

在 iOS 8 中,您还可以使用 UIPresentationController,它为您提供更多自定义选项。

于 2014-09-22T07:47:52.007 回答
4

您可以在呈现模态视图后调整它的框架:

在 iOS 5.1 - 6.1 中测试,使用 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.

更新首选的 iOS 6.0 视图控制器演示方法也可以正常工作:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
于 2010-11-24T20:51:07.107 回答
3

对于 iOS 8,只需在每个视图控制器上实现委托方法 (CGSize)preferredContentSize。它应该解决所有大小问题。

于 2014-09-25T23:56:30.893 回答
2

只是为了扩展 Fatos 解决方案(伟大的一个)如果您在 alloc initWithNibName 之后使用 .xib 文件创建视图控制器,您可以存储视图框架:

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

然后将其用于 superview.bounds,因此将使用 .xib 中的视图大小,您可以更直观地更改大小。

于 2011-10-18T17:30:14.060 回答
-1

我将此代码放在表单视图控制器中:

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

请注意,调整大小发生得足够早,以使演示动画看起来正确。

于 2012-07-22T22:43:40.490 回答