0
votes

I have a custom UIView which uses autolayout programatically to set the size of the frame. For this purpose, I set a constraint on the width property of the view to be equal to that of the superview and then a constraint of the aspect ratio to be some hard coded value

//width constraint
NSLayoutConstraint *widhtConstraint=[NSLayoutConstraint constraintWithItem:entryView
                                                                 attribute:NSLayoutAttributeWidth
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:scrollView
                                                                 attribute:NSLayoutAttributeWidth
                                                                multiplier:1.0f
                                                                  constant:8.0f];

[scrollView addConstraint:widhtConstraint];

//aspect ratio constraint
NSLayoutConstraint *aspectRatioConstraint=[NSLayoutConstraint constraintWithItem:entryView
                                                                       attribute:NSLayoutAttributeWidth
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:entryView
                                                                       attribute:NSLayoutAttributeHeight
                                                                      multiplier:80.0/27.0//aspect ratio same as formula view
                                                                        constant:0.0f];

[scrollView addConstraint:aspectRatioConstraint];

Please refer image:

Constraint setup

I wish to change the aspect ratio of this frame on touch of a button(View More) by increasing its height and then later resize it back to original on touching the same button.Additionally how do I figure out the total height of the view governed by all its subviews such that each subview is visible without clipping.(Basically the standard collapse feature)

2
How do I manipulate the aspect ratio constraint such that it contains all the subviews under it?Nikhil Verma
Can you suggest how I could dynamically change the 'aspectRatioConstraint' multiplier property(which in this case tells the aspect ratio of the view) . Being new to Autolayout, I couldn't find anything online either.Nikhil Verma

2 Answers

0
votes

You can have the aspect ratio as a variable in your code and have the code to set the constraints in a method such as updateConstraints.

float aspectRatio; NSLayoutConstraint *aspectRatioConstraint=[NSLayoutConstraint constraintWithItem:entryView
                                                                   attribute:NSLayoutAttributeWidth
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:entryView
                                                                   attribute:NSLayoutAttributeHeight
                                                                  multiplier:self.aspectRatio//aspect ratio same as formula view
                                                                    constant:0.0f];

Then when the button is pressed, in the action method you can modify the self.aspectRatio as fit and then call setNeedsUpdateConstraints and subsequently setNeedsLayout.

0
votes

I haven't understood exactly what the question is, but changing constraints in response to a button press so as to expand / collapse a superview is easy:

enter image description here

If that is the kind of thing you are after, you can find a downloadable example project here: https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/bk2ch04p183animationAndAutolayout4