【问题标题】:Custom size for Modal View loaded with Form Sheet presentation使用表单演示文稿加载的模态视图的自定义大小
【发布时间】:2013-05-07 06:11:40
【问题描述】:

我正在尝试通过表单演示在 iPad 中加载 UIViewController。问题是这个新视图的大小,我将大小值放在 IBuilder 中,但模态视图采用固定值。

我也尝试像这样在 prepareForSegue 中进行此操作:

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

但是不工作,有什么帮助吗?谢谢!

【问题讨论】:

  • 嗨,我在模态视图的 viewWillAppear 方法中设置了自定义大小:[self.view.superview setBounds:CGRectMake(0, 0, 200, 200)];但是现在,窗口没有以布局为中心
  • stackoverflow.com/questions/25787946/… 点此链接即可获得解决方案

标签: ios objective-c ipad uipresentationcontroller


【解决方案1】:

对于 iOS 8,使用:

self.preferredContentSize = CGSizeMake(width, height);

我已经把它放在viewDidLoad

斯威夫特 3

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

【讨论】:

  • 如何在 Interface Builder 中设置?
  • 在IOS 8迁移中,如何更改位置?
  • preferredContentSize 在 iOS 7 中引入
  • 也适用于 iOS9+
  • 为此,我必须使用 .modalPresentationStyle = .formSheet
【解决方案2】:

在属性检查器中检查Use Preferred Explicit Size

【讨论】:

  • 如果您将 Presentation 设置为 Form Sheet,这将起作用。
  • 作为表单呈现时如何降低黑色透明度?
【解决方案3】:

在 iOS 11 上设置 preferredContentSize 对我不起作用,例如在演示 EKEventEditViewController 时。

只有当我覆盖 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 消除你的可选
【解决方案4】:

编辑

使用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;
}];

【讨论】:

  • 至少在 iOS 7 中这是行不通的 - 它显示它是否呈现全尺寸,然后它会缩小它。正如“完成”块所证明的那样。
  • 要在 iOS7 上执行此操作,请查看此 stackoverflow.com/a/19026882/1363634
【解决方案5】:

我像@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;
}

【讨论】:

  • 在方向变化中产生问题
【解决方案6】:

我的解决方案很大程度上基于此处发布的其他解决方案,但由于我必须尝试许多不同的方法才能使其真正发挥作用,所以我将其发布。我的适用于带有 Swift 3.1 的 iOS 10,只有纵向模式的应用程序。 (未在横向模式下测试)我的解决方案的目标是显示一个比呈现视图小的模式,这样我就可以在背景中看到呈现视图。

我必须在 Interface Builder 中正确配置 UIViewController,否则我的代码将无法工作。这是我的设置。我发现我的解决方案可以同时使用 Cross DissolveCover Vertical 过渡样式。我认为我在 IB 中的关键是用于演示的 Over Full Screen - 它似乎不适用于此设置的其他一些值。

这是我想以模态方式呈现的 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 ScreenForm Sheet 的演示风格不同,因此即使您的回答实际上可能对使用 Over Full Screen 得到更好服务的人有所帮助,问题是专门针对后者。有关所有模态演示样式选项的列表,请参阅:developer.apple.com/documentation/uikit/…
【解决方案7】:

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

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

我从情节提要中全部设置并使用了 segue,如果使用代码,您也需要设置它。

parentViewController.modalPresentationStyle = UIModalPresentationPageSheet;

【讨论】:

    【解决方案8】:

    如果您在 XIB 中使用设置为视图控制器的模态呈现视图的视图。解决此问题的一个非常简单的方法是将 Size Metrics 更改为 Freeform(参见图 1.0)并将视图调整为您想要的任何大小。加载模式时,视图将显示这些指标。

    设置首选内容大小对我的情况没有帮助,但这解决了我的问题。它

    图1.0:

    【讨论】:

      【解决方案9】:

      我也有这个问题,你应该在展示后调整superview的框架。

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

      【讨论】:

        【解决方案10】:

        Erich 的帖子对我有用,但在带有 Swift 3 的 iOS 10 上,我不得不使用:

        self.preferredContentSize = CGSize(width, height)
        

        我也放在viewdidload

        另外,就像 avdyushin 所说,我必须检查 Use Preferred Explicit Size 框。

        【讨论】:

          【解决方案11】:

          一个非常简单的方法是在 SheetViewController 类中编辑这个属性

          sheetController = SheetViewController(controller: paymentVC, sizes: [.fixed(400), .fixed(400)])
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-18
            • 2015-03-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-06
            相关资源
            最近更新 更多