Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout aspect ratio based on `intrinsicContentSize` (not constant)

Is it possible to apply such auto layout constraints with aspect ratio calculated on the fly based on intrinsicContentSize? In the documentation I've found only constrains with fixed ratio value.

Actual with and height from intrinsicContentSize is not important in my use case, I want preserve height and width ratio of the view which changes dynamically.

Should I provide my own implementation of the constraint? Or is there a better way?

like image 385
Marek R Avatar asked Nov 10 '22 14:11

Marek R


1 Answers

The intrinsicContentSize is not available as input to constraints. (Logically, there are constraints that implement the intrinsicContentSize and related content-hugging and compression-resistance priorities, but that's different.)

If you want such an aspect ratio constraint, you'll have to add it yourself. It's easy enough to query the intrinsicContentSize at a given moment, verify that it provides real values (not NSViewNoInstrinsicMetric) for both dimensions, compute the aspect ratio, and then use that as the multiplier in a constraint that relates the item's width to its height.

The hard part is knowing when the intrinsicContentSize has been invalidated so you can remove the old aspect ratio constraint and add a new one. You can do that as a subclass of the view by overriding -invalidateIntrinsicContentSize. (Be sure to call through to super!) However, I don't know of a way to do that from a controller or superview.

like image 90
Ken Thomases Avatar answered Nov 15 '22 05:11

Ken Thomases