Dynamically change aspect ratio controlled by autoplay

Problem

In the UI, I have an idea of ​​what kind of content is stretchable but needs to maintain some aspect ratio. This aspect ratio changes at run time, and the layout of the views must update when some property that describes the format change changes.

I have exposed NSLayoutConstraint

, but its property used for aspect ratio: multiplier

is read-only. A funnier analogous property constant

can be changed.

Possible solutions

  • I could just use a constraint that captures the width or height and calculates it based on the expected ratio, but that requires additional boiler plate code to react to geometry / layout changes, and I prefer to avoid that.
  • I could experiment with intrisicContentSize

    and sizeThatFits:

    , but I'm not sure how this should be used withNSLayoutConstraint

+3


source to share


1 answer


Okay, I miss a lot. Removing and recreating constraints is not such a big deal (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];
}

      

+10


source







All Articles