基于旋转更新约束(X,Y,W,H)时出错?

时间:2015-07-31 10:24:46

标签: ios autolayout visual-format-language

这里我使用Visual Format Language来处理AutoLayouts。 如果我将方向从风景恢复为纵向 - 视图显示不正确。(图像(缩小)和第二个标签(Yposition))。即使在纵向模式下,我也没有在调试器窗口上获得任何约束错误消息。但如果我切换到横向模式 - 它的打印“无法同时满足约束。 “

我使用的代码如下:

-  (void)addConstraints {

[self.view removeConstraints:self.view.constraints];
NSDictionary *views = NSDictionaryOfVariableBindings(_contactImageProperty,_peopleAllowLabelProperty,_shareCodesDefaultMessageLabelProperty,_continueBtnProperty);
_contactImageProperty.translatesAutoresizingMaskIntoConstraints = NO;
_peopleAllowLabelProperty.translatesAutoresizingMaskIntoConstraints = NO;
_shareCodesDefaultMessageLabelProperty.translatesAutoresizingMaskIntoConstraints = NO;
_continueBtnProperty.translatesAutoresizingMaskIntoConstraints = NO;

// contactImageProperty
NSArray *constraints = [NSArray arrayWithObject:[NSLayoutConstraint constraintWithItem:_contactImageProperty attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0f]];

constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_contactImageProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:200]];

constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[_contactImageProperty]" options:0 metrics:nil views:views]];


// peopleAllowLabelProperty
constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_peopleAllowLabelProperty attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];

constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_peopleAllowLabelProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:50]];

constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_contactImageProperty]-10-[_peopleAllowLabelProperty]" options:0 metrics:nil views:views]];


// shareCodesDefaultMessageLabelProperty

constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_shareCodesDefaultMessageLabelProperty attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];

constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_shareCodesDefaultMessageLabelProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:100]];

constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_shareCodesDefaultMessageLabelProperty]-|" options:0 metrics:nil views:views]];

constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_peopleAllowLabelProperty]-100-[_shareCodesDefaultMessageLabelProperty]" options:0 metrics:nil views:views]];


// continueBtnProperty

constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_continueBtnProperty attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];

constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_continueBtnProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:50]];

constraints = [constraints arrayByAddingObject:[NSLayoutConstraint constraintWithItem:_continueBtnProperty attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:300]];

constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_continueBtnProperty]-10-|" options:0 metrics:nil views:views]];

[self.view addConstraints:constraints];

}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

NSDictionary *views = NSDictionaryOfVariableBindings(_peopleAllowLabelProperty,_shareCodesDefaultMessageLabelProperty,_continueBtnProperty);
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_contactImageProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:200.0f]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_peopleAllowLabelProperty]-200-[_shareCodesDefaultMessageLabelProperty]" options:0 metrics:nil views:views]];
}
else {
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_contactImageProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:100.0f]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_shareCodesDefaultMessageLabelProperty attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:50]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_peopleAllowLabelProperty]-20-[_shareCodesDefaultMessageLabelProperty]-[_continueBtnProperty]" options:0 metrics:nil views:views]];
}
[self.view setNeedsLayout];
[self.view setNeedsUpdateConstraints];

}

Portrait Orientation

Landscape Orientation

Issue Screen

1 个答案:

答案 0 :(得分:1)

对于图像缩小,您应该根据高度值通过在图像宽度上添加约束来强制图像的纵横比。

对于y位置,您应该添加3个隐藏视图,这些视图具有相同的高度约束以及相同的尾部和标题空间到图像,第一个标签,第二个标签和按钮。这样的事情:

Image
|
hidden view
|
label 1
|
hidden view
|
label 2
|
hidden view
|
button

隐藏视图的等高:类似[hiddenView1(== hiddenView2)]

隐藏视图和元素之间的平等空间:lile v:[topView] -5- [hiddenView] -5- [bottomView]

如果有帮助,请告诉我。