UIModalPresentationFormSheet调整大小视图

时间:2010-11-24 12:08:03

标签: ipad modal-dialog presentation uimodalpresentationformsh

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

因此,modalPresentationStyle具有固定的视图大小或完整视图,而我正介于两者之间。

6 个答案:

答案 0 :(得分: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);

答案 1 :(得分:4)

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

使用XCode 4.62

在iOS 5.1 - 6.1中测试
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

答案 2 :(得分: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,它为您提供更多自定义选项。

答案 3 :(得分:3)

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

答案 4 :(得分:2)

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

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

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

答案 5 :(得分:-1)

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

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

请注意,调整大小足够早,演示动画看起来是正确的。