문제

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.

도움이 되었습니까?

해결책

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];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top