Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoresize UIPopoverController using autolayout

I'm trying to use autolayout to automatically resize my popover to fit it's contents. I have fixed popover width but to compute height i rely on systemLayoutSizeFittingSize: passing my predefined width and zero height e.g. CGSizeMake(190, 0).

ContentController* controller = [ContentController new];
CGSize preferredSize = [controller.view systemLayoutSizeFittingSize:CGSizeMake(190, 0)];
controller.preferredContentSize = preferredSize;
UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:controller];
//popover presentation.

So far, so well, my current ContentController view hierarchy is something like (setup in the Interface Builder):

UILabel - multiline header (dynamically resized)
   |
UIImage with fixed width / height (static size)
   |
UILabel - multiline body (dynamically resized)

Thus, i just plug in my header / body text, call systemLayoutSizeFittingSize and it returns valid size that fits all the content of the view.

Dynamic height label with short text

Dynamic height label with long text

The problem arises when i try to put my body label inside UIScrollView. (Both in the IB and in code). Dynamic height is broken with scrollview

From now on, systemLayoutSizeFittingSize will not take body label height into account and will return height for header + image. I've setup all the constraints in the IB to support Pure Autolayout approach.

I've checked scrollview's content size - it is indeed equals to body label's intrinsic size, but scroll view's frame is squashed into 0.
I've checked and tried to reset label's maxPreferredLayoutWidth to the width of the content view, but it doesn't seems to affect anything.

I've set translatesAutoresizingMaskIntoConstraints on every view to NO, but it has no effect. I've set hugging / compression resistance priorities of both label and it's scrollview to 1000, but no luck. Setting them on the container view doesn't work either.

Here are screenshots of my IB view setup:

Label setup

ScrollView with inner label setu

My guess that is is somehow related to popover hosting views and their autolayout constraints, but i'm not sure.

I'm updating my labels via simple

_textContainerHeaderLabel.text = headerText;
_textContainerBodyTextLabel.text = bodyText;


[self.view setNeedsUpdateConstraints];
[self.view layoutSubviews];

So, the main question - how do i compute view's optimal fitting size via autolayout when it has UIScrollViews in it?

like image 745
Oladya Kane Avatar asked Oct 02 '22 02:10

Oladya Kane


1 Answers

Finally found a solution for this case. Don't know how correct it is, but i did the following - I've added height inequality constraint (>=0) from label to it's scrollview. The trick is to make this constraint's priority lower than label's compression resistance (vertical, in my case). This seems to to solve this problem.

like image 100
Oladya Kane Avatar answered Oct 13 '22 10:10

Oladya Kane