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 t
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 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.
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;
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:
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;
}