0
votes

I've got a view with two vertical constraints, a 1:1 aspect ratio costraint and a center alignment so it gets automatically resized depending on height of the screen. Now, this view is filled with a bunch of smaller subviews in the form of UIImageViews. I'd like these image views to automatically resize proportional to the superview but can't figure out how to do that. I've tried countless versions of constraints inside of the superview but all of them ended in a mess. Any ideas?

1

1 Answers

1
votes

Here is an example with 1 UIImageView:

first set the position of imageView in superview(for simplicity I chose upper left corner):

NSDictionary* views = NSDictionaryOfVariableBindings(orangeView);

[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:views]];
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView]"
                                                                options:0
                                                                metrics:nil
                                                                  views:views]];

after that set the width and height of the imageView to be relative to its superview:

[superview addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:redView
                                                      attribute:NSLayoutAttributeHeight
                                                     multiplier:0.4//between 0.0 and 1.0
                                                       constant:0]];

[superview addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                    attribute:NSLayoutAttributeWidth
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:redView
                                                    attribute:NSLayoutAttributeWidth
                                                   multiplier:0.4//between 0.0 and 1.0
                                                     constant:0]];