2

我想将 UIPickerView 隐藏在屏幕下方,在本例中为 3.5 英寸显示屏。在了解了约束值之后,我终于知道我应该输入什么数字了NSLayoutAttributeTop

这是我的完整代码:

NSLayoutConstraint *constraint = [NSLayoutConstraint
                                  constraintWithItem:_pickerView
                                  attribute:NSLayoutAttributeTop
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:self.view
                                  attribute:NSLayoutAttributeTop
                                  multiplier:1.0f
                                  constant:416.f];

[self.view addConstraint:constraint];

当我遇到我的 iDevice 时,它​​会像我预期的那样工作,除了......调试器控制台......它给了我这个消息:

2013-09-19 12:34:01.850 Constrain2[3546:c07] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x75490d0 h=--& v=--& V:[UIView:0x715a2f0(416)]>",
    "<NSLayoutConstraint:0x7159950 V:[UIPickerView:0x715a030(216)]>",
    "<NSLayoutConstraint:0x71597c0 V:|-(416)-[UIPickerView:0x715a030]   (Names: '|':UIView:0x715a2f0 )>",
    "<NSLayoutConstraint:0x715a7a0 UIPickerView:0x715a030.bottom == UIView:0x715a2f0.bottom>"
)

当我将常量值更改为取自 416(superview)-216(uipickerview)的 = 200.f 时,调试器控制台很清晰。那里没有消息。

是错误消息还是警告?可以忽略吗?是否可以隐藏屏幕下方的 UIPickerView 而不会在调试器控制台上显示该消息?

当 UIPickerView 正确放置在其位置时,似乎不会出现该消息。这是恒定的:200.f

谢谢你。

4

2 回答 2

1

不,你不应该忽略这一点——系统会尝试通过删除一个约束来修复它,但我不知道它是否总是会删除同一个约束,并给你想要的结果。您似乎正在添加一个与您已经拥有的约束相冲突的约束(我在对此的回答中评论了相同的问题你之前的问题)。当您在代码中添加新约束时,您需要删除您在 IB 中创建的一个(或多个)会与之冲突的约束。但是,要做你想做的事情,你甚至不应该添加一个新的约束,你应该修改你在 IB 中所做的那个。从错误消息看来,您从选择器底部到其父视图底部(长度为 0)有一个约束。您应该为该约束创建一个 IBOutlet(例如,我们将其称为 bottomCon),然后只需在代码中修改其常量参数:

self.bottomCon.constant = -216; 

在任何情况下,如果您要对靠近屏幕底部的视图进行约束,则应该将约束设置在底部,而不是像您在问题中所做的那样。如果将常量设置为顶部,则在不同的屏幕尺寸或方向上将不正确。

于 2013-09-19T15:24:25.180 回答
1

添加行

_pickerView.translatesAutoresizingMaskIntoConstraints = NO;

在添加约束之前。

于 2013-09-19T06:32:15.103 回答