Popover size to fit content

Hi everybody,

I'm facing with a hard problem.

I'm having a view with some labels inside.

The width of the view is fixed to 500 and I want the height to be dynamic, depending on content.

One of those labels is multiline, so it has dynamic height (with a max height set to 100).

I need to show the view in a popover and I want the bound of the popover to fit its content, for every heght the multiline label can have.

I know that popover size depends on the preferredContentSize property.


How can I compute the exact size for the preferredContentSize property?


I tryed

  • to compute that in viewDidLoad and viewWillAppear, but in both of them the height of children views was not yet be computed.
  • to call sizeToFit() on every view, but the height of multiline label was not computed taking into account the max height constraint.
  • to compute that in the viewDidLayoutSubviews(), but when I set the preferredContentSize, an infinite loop occurs


Thank you

Have you tried to override preferredContentSize getter?

You can send a - systemLayoutSizeFittingSize: message to the top-level view of your popover, passing the popover's current width and a zero height as the size argument. Set your view controller's preferredContentSize to the returned CGSize.

Even after doing as suggested by Frameworks Engineer the height returned is zero.

Popover size to fit content
 
 
Q