Custom size for Modal View loaded with Form Sheet presentation

只谈情不闲聊 提交于 2019-12-02 20:05:38
Erich

For iOS 8, use:

self.preferredContentSize = CGSizeMake(width, height);

I've placed this in viewDidLoad.

Swift 3

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

In Attributes inspector check Use Preferred Explicit Size

EDIT

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

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

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

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

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

Edited

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

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

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.

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 Dissolve and Cover Vertical transition styles. I think the key for me in IB was the Over Full Screen for Presentation - it didn't appear to work with some other values for this setting.

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, didLayout initialized as 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
        }
    }

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.

// 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.

parentViewController.modalPresentationStyle = UIModalPresentationPageSheet;

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);

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

self.preferredContentSize = CGSize(width, height)

I too placed it in viewdidload

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

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.

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

Figure 1.0:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!