iPad定制尺寸的模态视图控制器

eme*_*gro 54 uiviewcontroller ipad modalviewcontroller

我有一些特定尺寸的模态视图控制器.我试图避免使用自定义视图(在当前视图上创建全屏黑色半透明覆盖,在该视图上添加模态视图,执行动画等)来呈现它,因为没有适合我的大小的modalPresentationStyle控制器.

现在我正在使用UIModalPresentationPageSheet,但我的视图高度较小,我有一个丑陋的空白

期望的演讲

 _______________
|    _______    |
|   |       |   |
|   |  MyVC |   |
|   |       |   |
|    -------    |
 --------------- 
Run Code Online (Sandbox Code Playgroud)

实际演示

 _______________
|   |       |   |
|   |  MyVC |   |
|   |       |   |
|   |-------|   |
|   | blank |   |
 ---------------    
Run Code Online (Sandbox Code Playgroud)

如果我使用UIModalPresentationFormSheet,则容器的宽度较小.

我想弄清楚怎么做,但我不知道是否可能.呈现比任何presentationStyles更小的模态VC的问题的解决方案是什么?唯一的解决方案是安排"自定义模态视图控制器引擎"?弹出装置不符合我的设计要求:(

Len*_*are 84

和其他一些用户一样,我也遇到了模态视图控制器的起源不正确的问题.经过一些实验,我找到了一个适合我的解决方案:

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];   
    self.view.superview.bounds = CGRectMake(0, 0, <width>, <height>);
}
Run Code Online (Sandbox Code Playgroud)

  • 这适合我.它正确地将重新调整的表单放在iOS 6中. (3认同)
  • 当您在视图中放置至少1个可编辑的UITextField时,它不起作用.当您尝试进入编辑模式时,框架会尝试显示键盘,并且系统会进入无限循环! (3认同)
  • 适用于iOS 7 (2认同)

mar*_*cio 35

我使用以下方法获得了以下结果(链接文本):

self.modalPresentationStyle = UIModalPresentationFormSheet;
Run Code Online (Sandbox Code Playgroud)

并将其作为模态视图控制器呈现.如果您需要进一步的帮助,请告诉我.

  • 谢谢你的答案,但FormSheet不符合我的规模(我在问题中说). (4认同)

paw*_*l_d 24

所有这些答案都不适用于ios8 SDK.

这将有效:

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

在AboutViewController.m中

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];

    if(!IS_IOS8)
    {
        self.view.superview.bounds = CGRectMake(0, 0, 300, 300);
    }
}
Run Code Online (Sandbox Code Playgroud)

IS_IOS8

#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
Run Code Online (Sandbox Code Playgroud)

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


Bre*_*den 19

我有一个问题是让自定义大小的模态居中,直到我弄清楚ViewController.view.superview.frame最初设置的内容.它是根据UIModalPresentation类型确定的.superview.center甚至不需要(据我的测试显示).

此外,我动态设置坐标[UIScreen mainScreen].applicationFrame,现在只支持横向.您必须通过翻转X/Y和高度/宽度值来进行条件检查以支持纵向和横向.

这是我的第一个iOS 6.0工作解决方案:

ViewController.modalPresentationStyle = UIModalPresentationFormSheet;
ViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:ViewController animated:YES completion:nil];
ViewController.view.superview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
ViewController.view.superview.frame = CGRectMake(
                                                                    // Calcuation based on landscape orientation (width=height) 
                                                                    ([UIScreen mainScreen].applicationFrame.size.height/2)-(320/2),// X
                                                                    ([UIScreen mainScreen].applicationFrame.size.width/2)-(320/2),// Y
                                                                    320,// Width
                                                                    320// Height
                                                                    );
Run Code Online (Sandbox Code Playgroud)

然后我看着前面的答案,脸上露出了脸.通过将最后一行替换为以下内容来缩短我的解决方案:

ViewController.view.superview.bounds = CGRectMake(0, 0, 320, 320);
Run Code Online (Sandbox Code Playgroud)

编辑最终解决方案和备注.


Rob*_*nes 18

执行此操作的最佳方法是更改​​在表单内显示的视图的超级视图上的bounds属性.当您以模态方式呈现视图时,呈现的视图将嵌入到另一个视图中.

您应该将superview的bounds属性设置为与视图边界相同的rect.首先在你的班级添加一个私人ivar:

@implementation MySpecialFormsheet {
    CGRect _realBounds;
} 
Run Code Online (Sandbox Code Playgroud)

最好这样做viewWillAppear:.将以下代码添加到以模态方式呈现的视图控制器中:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.view.superview.bounds = _realBounds;
}
Run Code Online (Sandbox Code Playgroud)

您需要_realBoundsviewDidLoad方法中缓存:

- (void)viewDidLoad {
    _realBounds = self.view.bounds;
    [super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)

  • 不确定早期版本,但从iOS 6.0开始,视图不再居中.x坐标始终从正常表单所在的位置开始.用self.view.center或`self.view.superview.center`似乎无法修复. (3认同)

Mag*_*zio 7

这是我的自动布局视图的解决方案(从未尝试使用自动布局)并且符合iOS 7和iOS 8.

首先,请务必以这种方式调用模态视图:

CloudSettingsViewController *cloudController = [[CloudSettingsViewController alloc] initWithNibName:@"CloudSettingsView" bundle:nil];

CGRect originalBounds = cloudController.view.bounds;

cloudController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
cloudController.modalPresentationStyle = UIModalPresentationFormSheet;

[self presentViewController:cloudController animated:YES completion:nil]; 
Run Code Online (Sandbox Code Playgroud)

使用iOS 8在模态视图的viewDidLoad方法中设置preferredContentSize.

- (void)viewDidLoad {
    [super viewDidLoad];

    // backup of the original size (selected in interface builder)
    height = self.view.frame.size.height;
    width = self.view.frame.size.width;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) // versioni successive ios 8
        self.preferredContentSize = CGSizeMake(width, height); // this is necessary to get the correct size of the modal view
}
Run Code Online (Sandbox Code Playgroud)

当模态视图需要在iPad上显示键盘时,这也适用.

使用以前版本的iOS 8,您应该在presentViewController调用之后设置边界:

[currentController presentViewController:controlPanelController animated:YES completion:nil];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8) 
    cloudController.view.superview.bounds = originalBounds;//it's important to do this after 
Run Code Online (Sandbox Code Playgroud)


Kas*_*ame 6

即使是减少您可以使用的地层板高度和宽度

[self.view.superview setBounds:CGRectMake(0, 0, 450, 480)];
Run Code Online (Sandbox Code Playgroud)


hat*_*nch 5

上述方法需要针对iOS7进行调整:

// set desired bounds, then call -presentFromViewController:

@implementation PresentedViewController
{
    CGRect _presentationBounds;
}

- (void)presentFromViewController:(UIViewController *)viewController
{
    self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    self.modalPresentationStyle = UIModalPresentationFormSheet;

    _presentationBounds = self.view.bounds;

    [viewController presentViewController:self animated:YES completion:nil];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    if (!CGRectIsEmpty(_presentationBounds))
    {
        self.view.superview.bounds = _presentationBounds;
        _presentationBounds = CGRectZero;
    }
}
Run Code Online (Sandbox Code Playgroud)

(此代码也适用于iOS6.)