为什么这些限制不起作用?

时间:2013-03-25 18:14:56

标签: iphone ios uiscrollview constraints autolayout

我有一个我想在viewDidLoad方法中添加到我的superview的scrollview。我使用以下代码执行此操作:

[self.view addSubview:self.scroll];
NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:4];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.scroll
                             attribute:NSLayoutAttributeLeft
                             relatedBy:NSLayoutRelationEqual
                                toItem:self.view
                             attribute:NSLayoutAttributeLeft
                            multiplier:1
                              constant:0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.scroll
                                                    attribute:NSLayoutAttributeRight
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:self.view
                                                    attribute:NSLayoutAttributeRight
                                                   multiplier:1
                                                     constant:0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.scroll
                                                    attribute:NSLayoutAttributeBottom
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:self.view
                                                    attribute:NSLayoutAttributeBottom
                                                   multiplier:1
                                                     constant:0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.scroll
                                                    attribute:NSLayoutAttributeHeight
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:self.view
                                                    attribute:NSLayoutAttributeHeight
                                                   multiplier:0.5
                                                     constant:0]];

[self.view addConstraints:constraints];

scrollview旨在放置在视图的底部,它的高度应该是superview高度的一半。

我收到以下错误:

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) 

(     “”     “”     “”     “” )

将尝试通过违反约束来恢复

有人可以告诉我我做错了什么吗?谢谢。

1 个答案:

答案 0 :(得分:2)

rdelmar解决了这个问题:

您可以尝试添加[self.scroll setTranslatesAutoresizingMaskIntoConstraints:NO]