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

Posted

技术标签:

【中文标题】基于旋转更新约束(X,Y,W,H)时出错?【英文标题】:Error while updating the Constraints(X,Y,W,H) based on Rotation? 【发布时间】:2015-07-31 10:24:45 【问题描述】:

这里我使用视觉格式语言来处理自动布局。 如果我将方向从横向恢复为纵向 - 视图显示不正确。(图像(收缩)和第二个标签(Y 位置))。即使在纵向模式下,我也没有在 调试器窗口 上收到任何约束错误消息。但如果我切换到横向模式 - 它的打印“无法同时满足约束。 "

我使用了以下代码:

-  (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];

【问题讨论】:

【参考方案1】:

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

对于 y 位置,您应该向图像、第一个标签、第二个标签和按钮添加另外 3 个具有相同高度约束和相同尾随和标题空间的隐藏视图。类似的东西:

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

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

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

如果这有帮助,请告诉我。

【讨论】:

实际上我想恢复位置(Label2)和大小(ImageView)。所以我想在横向模式下减少 UIImageView 的高度。而变成肖像的高度将是一个olderValue。而第二个标签将是横向中的 Y 位置不同,因为 UIScreeen Height!=Portrait Mode.height。所以,这里我们必须改变 Y 值和什么时候变成人像。?注意:我不想丢失任何元素 对于imageView,你做的很好,但你只改变高度,宽度保持不变,图像缩小。所以只需为宽度添加一个约束,如比率*高度。这应该可以。尽量避免用静态值来思考,尽量用比例来工作。如果您使用我在回答中解释的内容,您应该在纵向和横向上都有一个相当好的视图分布,而不必为每种尺寸的屏幕方向保持价值。如果这不是您所期望的,也许我们需要更多关于您想要的细节。 只是在问。在视觉格式语言中,是否可以在进行定向时更新约束?你能告诉我怎么做吗?因为如果集中横向,纵向模式约束将不适用。我不知道。所以这里为横向和纵向创建了两个不同的约束。但我想从现有约束中更新值。?请看这个例子:在这里你可以看到要达到的目标。 dl.dropboxusercontent.com/u/15307735/… 我更喜欢为此使用 Storyboard。刚刚对您的故事板进行了一些更改,您可以在此处找到 we.tl/6jrO1BgXTU 查看界面生成器中的预览(打开故事板旁边的助理编辑器>选择预览)并单击底部的添加按钮以添加其他屏幕和方向。告诉我这是你所期望的,还是接近它的东西。然后我们可以对其进行微调。告诉我 您刚刚添加了三个隐藏视图。您在 Debugger 窗口中注意到了吗?关于未满足的约束等很多问题。即使您只是进入横向模式。我需要在横向模式下查看所有元素。它是如何在肖像中看到的。如果你看到我的项目,我没有添加隐藏视图,我也实现了仪式。但是如果您添加更多视图,我们将无法查看所有元素仪式。我的意思是。如果你使用 ScrollView 你可以。但我不想使用 ScrollView。我只想更新 UImageView 和标签约束的位置和高度。

以上是关于基于旋转更新约束(X,Y,W,H)时出错?的主要内容,如果未能解决你的问题,请参考以下文章

基于旋转在X Y轴上移动

两个矩形相交

nodejs使用jimp实现图片处理

love2d角度,方向以及旋转

canvas做动画

JavaScript - 旋转基于SVG的线的角度