In UI I have some view which content is stretchable but should conserve some aspect ratio. This aspect ratio changes during run-time and views layout should be updated when some property describing aspect ratio changes.
I've exposed NSLayoutConstraint
, but its property used for aspect ratio: multiplier
is read only. What is more funny similar property constant
can be changed.
intrisicContentSize
and sizeThatFits:
but I'm not sure how this should be used with NSLayoutConstraint
Ok I'm whining to much. Removing and recreating constraint is not such a big problem (thanks @vacawama):
- (void)updateASpectRatio: (float)aspectRatio {
// self.aspectRatioConstraint - has to be a strong reference
[self.aspectRatioView removeConstraint: self.aspectRatioConstraint];
self.aspectRatioConstraint = [NSLayoutConstraint constraintWithItem: self.aspectRatioConstraint.firstItem
attribute: self.aspectRatioConstraint.firstAttribute
relatedBy: self.aspectRatioConstraint.relation
toItem: self.aspectRatioConstraint.secondItem
attribute: self.aspectRatioConstraint.secondAttribute
multiplier: self.aspectRatioView.aspectRatio
constant: 0.0];
[self.aspectRatioView addConstraint: self.aspectRatioConstraint];
}
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