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.

Était-ce utile?

La 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];
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top