Question

I'm very new with auto layout. I have a simple UIViewController that contains a View and a UIImageView.

On portrait mode this image is 320x115px but I would like to reduce the image height when rotating to landscape.

Currently I have the following constraints on my image. -Height Equals 115px -Trailing Space to superview -Leading Space to superview -Top Space to supverview

How to make my UIView have proportional vertical size when rotating from portrait to landscape?

Thanks for your help,

Sébastien.

Was it helpful?

Solution

If you want the same proportional size as 115 is to 480 (0.24), then you use the multiplier parameter in constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.

In the example below, I made an IBOutlet to the height constraint in IB (heightCon), then remove that in code and add another one that is based on a fraction of self.view's height (iv is my outlet to the image view):

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.iv removeConstraint:self.heightCon];
    NSLayoutConstraint *newHeightCon = [NSLayoutConstraint constraintWithItem:self.iv attribute:NSLayoutAttributeHeight relatedBy:0 toItem:self.view attribute:NSLayoutAttributeHeight multiplier:.24 constant:0];
    [self.view addConstraint:newHeightCon];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top