Neal.Marlin
Neal.Marlin

Reputation: 518

why preferredContentSize doesn't work when presenting UIViewController with PresentationStyle = .formSheet

I just try to test the preferredContentSize effect with code below:

let vc = CustomViewController()
vc.modalPresentationStyle = .formSheet
self.preferredContentSize = CGSize(width:50, height: 50)
self.present(vc, animated: true)

But it seems that the preferredContentSize property doesn't work at all. I can definitely sure that "CustomViewController" have no side effect on this problem because it's an empty UIViewController subclass.

According to the official docs:

In a regular-width, regular-height size class, the system adds a dimming layer over the background content and centers the view controller’s content on top of this layer. The default content size is smaller than that of the UIModalPresentationStyle.pageSheet style. A part of the background content always remains visible. To provide a custom content size, use the modal view controller’s preferredContentSize property.

I've found a lot of similar questions, but nearly all of them are about presenting with popover. My question is whether preferredContentSize only works with .popover style, or .formSheet style either.

And if it works with .formSheet style presentation, what's wrong with my code? Any clue will be appreciated.

Upvotes: 1

Views: 469

Answers (1)

Joe Thomson
Joe Thomson

Reputation: 31

For iOS 15 and later see Warpling's answer here: https://stackoverflow.com/a/79118419/19705384

In summary:

viewController.sheetPresentationController?.prefersPageSizing = false

Upvotes: 1

Related Questions