Custom size for Modal View loaded with Form Sheet presentation

IosObjective CIpadUipresentationcontroller

Ios Problem Overview


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.

Also i tried to make this in prepareForSegue like this:

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

But don't work, any help? Thanks!

Ios Solutions


Solution 1 - Ios

For iOS 8, use:

self.preferredContentSize = CGSizeMake(width, height);

I've placed this in viewDidLoad.

Swift 3

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

Solution 2 - Ios

In Attributes inspector check Use Preferred Explicit Size

enter image description here

Solution 3 - Ios

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

enter image description here enter image description here

Solution 4 - Ios

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

Solution 5 - Ios

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

Solution 6 - Ios

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.

IB Settings

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

Solution 7 - Ios

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;

Solution 8 - Ios

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:

enter image description here

Solution 9 - Ios

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

Solution 10 - Ios

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.

Solution 11 - Ios

A very simple way is to edit this property in SheetViewController class

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFran FoxView Question on Stackoverflow
Solution 1 - IosErichView Answer on Stackoverflow
Solution 2 - IosavdyushinView Answer on Stackoverflow
Solution 3 - IosGleb TarasovView Answer on Stackoverflow
Solution 4 - IosToseef KhiljiView Answer on Stackoverflow
Solution 5 - IosorafaelreisView Answer on Stackoverflow
Solution 6 - IosJacob F. Davis C-CISOView Answer on Stackoverflow
Solution 7 - IosStuart CampbellView Answer on Stackoverflow
Solution 8 - IosPeter SuwaraView Answer on Stackoverflow
Solution 9 - IosSanth0shView Answer on Stackoverflow
Solution 10 - IosSomeGuyView Answer on Stackoverflow
Solution 11 - IosDiego SalcedoView Answer on Stackoverflow