我有一个UIImageView,我需要在改变方向和屏幕尺寸时扩展(高度和宽度)。我正在使用自动布局约束。展开自动布局中改变方向和屏幕尺寸的意见
topImageView.contentMode = UIViewContentModeScaleAspectFit;
topImageView.backgroundColor = [UIColor clearColor];
topImageView.layer.cornerRadius = 5.0f;
topImageView.clipsToBounds = YES;
topImageView.translatesAutoresizingMaskIntoConstraints = NO;
if(login_DO.logoPath)
[topImageView loadImage:login_DO.logoPath];
[self.view addSubview:topImageView];
NSArray *horizontalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-(%i)-[topImageView(%f)]",X_OFFSET,VIEW_FRAME_WIDTH-X_OFFSET*2]
options:0 metrics:nil views:@{@"topImageView": topImageView}];
NSArray *verticalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-(%f)-[topImageView(80)]",navHeight]
options:0 metrics:nil views:@{@"topImageView": topImageView}];
[self.view addConstraints:horizontalConstraints];
[self.view addConstraints:verticalConstraints];
NSLayoutConstraint *leadingMarginForImageConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeLeadingMargin
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view attribute:
NSLayoutAttributeLeadingMargin multiplier:1.0 constant:X_OFFSET];
NSLayoutConstraint *topMarginForImageConstraint = [NSLayoutConstraint
constraintWithItem:topImageView attribute:NSLayoutAttributeTopMargin
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.view attribute:
NSLayoutAttributeTopMargin multiplier:1.0 constant:VIEW_FRAME_WIDTH-X_OFFSET*2];
[self.view addConstraints:@[ leadingMarginForImageConstraint,
topMarginForImageConstraint]];
但图像没有扩大。我是新来的自动布局。我是否缺少任何约束?
任何意见或答案? – Nitish 2014-10-01 09:54:25
您的约束无效_______________ 假设x = 10;宽度= 300; _______________ left = -x;宽度> = -300。 _______________ 与高度相同,就像您在宽度和高度上翻转图像一样。 _______________ 解释更多你想要实现..会相应地添加一个答案。 – 2014-10-04 13:21:34
添加一个屏幕短.. ..如果可能解释。 – 2014-10-04 13:29:26