Search code examples
iosswiftautolayoutstoryboarduipopovercontroller

Popover adapt to content size IOS


I have simple popover on iPhone:

Popover

I would like to have its size be as "content size" of popup - ending just below add button. Can I make it happen by using auto layout? I would prefer not to change width of this popover, just height. I have tried to use prefferedContentSize for View inside, but it was then too small. I tried to do this by auto layout, and it did not work.

This is how it looks in storyboard:

Storyboard


Solution

  • You can calculate the width and height of the views in the popover (or use the default width in this case) in viewWillAppear(_:) and use that to set the preferredContentSize of the view controller. Waiting till viewWillAppear(_:) is important so the frames are set.