gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:47:23 25 4
gpt4 key购买 nike

这里我使用视觉格式语言来处理自动布局。 如果我将方向从横向恢复为纵向 - View 显示不正确。(图像(收缩)和第二个标签(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];

Portrait Orientation

Landscape Orientation

Issue Screen

最佳答案

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

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

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

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

隐藏 View 和元素之间的间距相等:有点像 V:[topView]-5-[hiddenView]-5-[bottomView]

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

关于ios - 基于旋转更新约束(X、Y、W、H)时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31743883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com