【发布时间】:2017-01-08 05:22:40
【问题描述】:
我有一个 iOS 9 和 10 应用程序,它在窗口底部有一个小“托盘”UIView,它可以滑入和滑出以显示(或隐藏)某些控件。默认情况下,托盘完全显示(“滑出”),因此用户可以看到所有内容。最右边有一个按钮,他们可以点击该按钮使托盘“滑入”到左侧,这样托盘大部分都被隐藏了,因为它现在在左侧屏幕外。 (最右边的 20 个点是用户可以点击以滑入/滑出的“拇指”,因此拇指是托盘滑入时唯一保持可见的部分。)
我想这样做的方法是通过 Interface Builder 定义两个不同的约束,我只是简单地切换它们,以便一次只有一个处于活动状态。例如,这两个约束可能是:
trayOutConstraint:
trayView.leading = (superview.leading * 1) + 0 // 结果 == 0
trayInConstraint:
trayView.leading = (superview.trailing * -1) + 20 // 结果 == -300(例如)
事实证明,我可以通过编程方式为 trayInConstraint 创建约束,如下所示(编辑:展开以显示我如何禁用情节提要的默认约束并添加我的新约束):
// Disable the default constraint (in the storyboard)
[[self trayTripOutLeadingConstraint] setActive:NO];
// Create the new constraint (add to an array by itself)
NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:(id)trayView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:(id)[trayView superview] attribute:NSLayoutAttributeTrailing multiplier:-1 constant:20];
NSArray *newTrayConstraintsArray = [[NSArray alloc] initWithObjects:newConstraint, nil];
// Remove any existing constraints array and use the new one
[[self view] removeConstraints:[self tripTrayConstraints]];
[self setTripTrayConstraints:newTrayConstraintsArray];
[[self view] addConstraints:newTrayConstraintsArray];
但如果我尝试在 Interface Builder 中创建它,我无法将乘数设置为 1.0 以外的任何值。 (键入任何其他值只会强制它重置为“1”。)
我在 Apple 文档中看到 https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/AnatomyofaConstraint.html:
您不能将非恒等乘数(1.0 以外的值)与 位置属性。
但是为什么我可以用代码而不是 IB 来创建呢?
(除了可能对其他人有用的无关之外:我还想创建一个约束,其中倍数为 0,但无法以编程方式或在 IB 中执行此操作。我发现我可以解决无法使用“0.0”的问题" 作为乘数,而是使用非常小的值,例如 0.000001。)
【问题讨论】:
-
你是如何分配新约束的,你能在这里告诉我吗?
-
我已经编辑了我的问题以包含创建和分配新约束的代码。但这不是我的问题。我的问题不是如何创建创建所需约束的代码。我已经知道了,如上面的代码所示。相反,我的问题是:为什么我不能为 Interface Builder 中的“乘数”分配一个值“-1”?
标签: ios objective-c xcode