ios 使用表单演示加载的模态视图的自定义大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16518175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:33:47  来源:igfitidea点击:

Custom size for Modal View loaded with Form Sheet presentation

iosobjective-cipaduipresentationcontroller

提问by Fran Fox

I'm trying to load a UIViewController in iPad with Form Sheet presentation. The problem is the size of this new view, i put the size values in the IBuilder but the modal view take a fixed value.

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

Also i tried to make this in prepareForSegue like this:

我也尝试在 prepareForSegue 中这样做:

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

But don't work, any help? Thanks!

但不起作用,有什么帮助吗?谢谢!

回答by Erich

For iOS 8, use:

对于 iOS 8,请使用:

self.preferredContentSize = CGSizeMake(width, height);

I've placed this in viewDidLoad.

我把这个放在viewDidLoad.

Swift 3

斯威夫特 3

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

回答by avdyushin

In Attributes inspector check Use Preferred Explicit Size

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

enter image description here

在此处输入图片说明

回答by Toseef Khilji

EDIT

编辑

Use preferredContentSize.

使用preferredContentSize.

Old

老的

You can try this for show view in center

您可以尝试在中心显示视图

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

回答by Gleb Tarasov

Setting preferredContentSizedidn't work for me on iOS 11 for example when presenting EKEventEditViewController.

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

It works only if I override getter of preferredContentSize. Something like this:

只有当我覆盖 .getter 时它才有效preferredContentSize。像这样的东西:

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
        }
    }
}

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by orafaelreis

I solved it like @Stuart Campbell and @Viruss mca.

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

Edited

已编辑

After @Erich's comment, I rewrote it to run in iOS 8 too. Below is the code:

在@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;
}

回答by Jacob F. Davis C-CISO

My solution is based heavily on other solutions posted here but since I had to try many different things to get it to actually work I am posting it. Mine is for iOS 10 with Swift 3.1 in a portrait-mode only app. (Untested in landscape mode) The goal of my solution was to display a modal that was smaller than the presenting view, and such that I could see the presenting view in the background.

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

I had to configure the UIViewController properly in Interface Builder or my code wouldn't work. Here are my settings. I found my solution to work with both Cross Dissolveand Cover Verticaltransition styles. I think the key for me in IB was the Over Full Screenfor Presentation - it didn't appear to work with some other values for this setting.

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

IB Settings

IB设置

Here is the code in the UIViewController I want to present modally. Then I just call it using present(_:animated:completion:)elsewhere. Also, in the VC I will present modally, I have a bool, didLayoutinitialized as false:

这是我想以模态方式呈现的 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
        }
    }

回答by Stuart Campbell

I got around this problem by putting my content inside a view in the modal vc. Then setting the vc background transparent and in viewWillAppear settings the formsheet background transparent. This means you only see the content.

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

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

I set it all from storyboard and used a segue, if using code you would need to set this also.

我从故事板中设置了所有内容并使用了 segue,如果使用代码,您也需要设置它。

parentViewController.modalPresentationStyle = UIModalPresentationPageSheet;

回答by Santh0sh

I also had this issue, You should resize superview's frame after presenting it.

我也有这个问题,您应该在呈现后调整超级视图的框架大小。

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

回答by SomeGuy

Erich's post worked for me, but on iOS 10 with Swift 3, I had to use:

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

self.preferredContentSize = CGSize(width, height)

I too placed it in viewdidload

我也把它放在 viewdidload

Also, like avdyushin said, I had to check the Use Preferred Explicit Sizebox.

另外,就像 avdyushin 所说的,我必须选中该Use Preferred Explicit Size框。

回答by Peter Suwara

If you are using a view in XIB that is set as a modally presented view for a view controller. A very easy way to fix this is to simply change the Size Metrics to Freeform(See Figure 1.0) and resize the view to whatever you want it to be. The view will appear with these metrics when loaded modaly.

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

Setting Preferred Content Size didn't help in my case but this fixed my issue. It

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

Figure 1.0:

图 1.0:

enter image description here

在此处输入图片说明