使用表单展示方式加载的Modal视图的自定义尺寸

53

我正在尝试在iPad上使用表单视图方式加载一个UIViewController。问题是这个新视图的大小,我已经在IBuilder中设置了大小值,但模态视图仍然采用固定值。

我也尝试过在prepareForSegue中这样做:

HelpViewController *viewController = segue.destinationViewController;
viewController.view.superview.frame = CGRectMake(0, 0, 200, 200);

但是不起作用,需要帮助吗?谢谢!


嗨,我在modal视图的viewWillAppear方法中设置了自定义大小,使用以下代码:[self.view.superview setBounds:CGRectMake(0, 0, 200, 200)];但现在,窗口与布局不居中。 - Fran Fox
1
请阅读此链接:https://dev59.com/k3E95IYBdhLWcg3wGqAH#4271364 - Mike Pollard
请查看此链接,您可以获得解决方案。 - Mohammed Hussain V
10个回答

86

对于 iOS 8,请使用:

self.preferredContentSize = CGSizeMake(width, height);

我已将此放置在 viewDidLoad 中。

Swift 3

self.preferredContentSize = CGSize(width: 100, height: 100)

1
你如何在Interface Builder中设置这个? - Blip
1
在iOS 8迁移中,我如何更改位置? - Vipin Vijay
preferredContentSize 在 iOS 7 中被引入。 - Antoine
适用于iOS9+。 - DevC
3
为了实现这个功能,我必须使用 .modalPresentationStyle = .formSheet。 - userx
显示剩余2条评论

23
在属性检查器中勾选“使用首选显示尺寸”。 在此输入图像描述

4
如果您将演示设置为“表单表”,则此功能有效。 - Artur Friesen
当呈现表单视图时,我该如何减小黑色的透明度? - Satyam

15

例如,在呈现 EKEventEditViewController 时,对我来说在 iOS 11 上设置 preferredContentSize 没有效果。

只有当我重写 preferredContentSize 的 getter 方法时才有效。像这样:

private class CLEventEditViewController: EKEventEditViewController {
    override var preferredContentSize: CGSize {
        get {
            if let fullSize = self.presentingViewController?.view.bounds.size {
                return CGSize(width: fullSize.width * 0.5,
                              height: fullSize.height * 0.75)
            }
            return super.preferredContentSize
        }
        set {
            super.preferredContentSize = newValue
        }
    }
}

输入图片说明 输入图片说明


通过使用 let fullSize = super.preferredContentSize,消除您的可选项。 - Grease

11

编辑

使用 preferredContentSize

旧的

你可以尝试这个方法来将视图显示在中心

HelpViewController * viewController = [[[HelpViewController alloc] init] autorelease];
viewController.modalPresentationStyle=UIModalPresentationFormSheet;
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:viewController animated:YES completion:^{
    viewController.view.superview.frame = CGRectMake(0, 0, 200, 200);
    viewController.view.superview.center = self.view.center;
}];

2
至少在iOS 7中,这不起作用 - 它会显示完整尺寸的呈现,然后缩小它。正如“完成”块所证明的那样。 - iOSProgrammingIsFun
2
要在iOS7上执行此操作,请查看此链接:https://dev59.com/vWMk5IYBdhLWcg3w0RGU#19026882 - CoderPug

3

我的解决方案在其他发布的解决方案的基础上进行了大量改进,因为我尝试了许多不同的方法才使它真正起作用。我的方案适用于仅支持竖屏模式的iOS 10和Swift 3.1应用程序(未经横向模式测试)。我的解决方案的目标是显示比呈现视图更小的模态视图,并且我可以在背景中看到呈现视图。

我必须在Interface Builder中正确配置UIViewController,否则我的代码将无法工作。以下是我的设置。我发现我的解决方案适用于交叉溶解和垂直覆盖转换样式。在IB中,对我来说关键是Presentation选项卡中的Over Full Screen - 对于此设置的某些其他值似乎不起作用。

IB Settings

这是我想要以模态方式呈现的UIViewController中的代码。然后我只需在其他地方使用present(_:animated:completion:)调用它。此外,在我要以模态方式呈现的VC中,我有一个名为didLayout的布尔变量,初始化为false:

override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if !didLayout {

            let width:CGFloat = self.view.bounds.width * 0.95 //modify this constant to change the amount of the screen occupied by the modal
            let height:CGFloat = width * 1.618 //golden ratio

            self.view.superview!.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.15) //slightly dim the background to focus on the modal
            let screen = self.view.superview!.bounds
            let frame = CGRect(x: 0, y: 0, width: width, height: height)
            let x = (screen.size.width - frame.size.width) * 0.5
            let y = (screen.size.height - frame.size.height) * 0.5
            let mainFrame = CGRect(x: x, y: y, width: frame.size.width, height: frame.size.height)

            self.view.frame = mainFrame

            didLayout = true
        }
    }

需要注意的是,“全屏幕”(Over Full Screen)和“表单表格”(Form Sheet)是不同的演示样式,因此即使您的答案实际上可以帮助那些更适合使用“全屏幕”的人,但问题特别针对后者。有关所有模态演示样式选项的列表,请参见:https://developer.apple.com/documentation/uikit/uimodalpresentationstyle - Magnus Lind Oxlund

2

我像@Stuart Campbell和@Viruss mca一样解决了它。

编辑

@Erich的评论后,我重新编写了代码以在iOS 8中运行。以下是代码:

  HelpViewController * viewController = [[[HelpViewController alloc] init]];
  viewController.modalPresentationStyle=UIModalPresentationFormSheet;
  [self presentViewController:viewController animated:YES completion:nil];

--

//HelpViewController.m

- (void) viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    if (!didLayout) {
        [self layout];
        didLayout = YES;
    }
}
- (void) layout{
    self.view.superview.backgroundColor = [UIColor clearColor];
    CGRect screen = self.view.superview.bounds;
    CGRect frame = CGRectMake(0, 0, <width>, <height>);
    float x = (screen.size.width - frame.size.width)*.5f;
    float y = (screen.size.height - frame.size.height)*.5f;
    frame = CGRectMake(x, y, frame.size.width, frame.size.height);

    self.view.frame = frame;
}

屏幕方向变化时出现问题 - Vipin Vijay

2
如果您在XIB中使用的视图被设置为模态呈现视图控制器,则解决此问题的一种非常简单的方法是将Size Metrics更改为自由形式(参见图1.0),并将视图调整为所需大小。当以模态方式加载视图时,它将显示这些指标。
在我的情况下,设置Preferred Content Size没有帮助,但这个方法解决了我的问题。
图1.0:

enter image description here


1

我通过将我的内容放在模态视图控制器中的视图内来解决了这个问题。然后将vc背景设置为透明,在viewWillAppear中设置表单背景透明。这意味着您只会看到内容。

// In Modal vc
- (void)viewWillAppear:(BOOL)animated
{
    self.view.superview.backgroundColor = [UIColor clearColor];
    [super viewWillAppear:animated];
}

我从storyboard上设置了所有内容,并使用了一个segue,如果使用代码,你也需要进行设置。

parentViewController.modalPresentationStyle = UIModalPresentationPageSheet;

1

Erich的帖子对我有用,但在iOS 10上使用Swift 3时,我必须使用:

self.preferredContentSize = CGSize(width, height)

我也将它放在viewdidload中。

另外,就像avdyushin所说,我不得不勾选使用首选显式大小框。


0

我也遇到过这个问题,你应该在呈现之后调整父视图的框架大小。

HelpViewController *viewController = segue.destinationViewController;
viewController.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:viewController animated:YES completion:nil];
viewController.view.superview.frame = CGRectMake(0, 0, 200, 200);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接