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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With