1

我有一个滚动视图,我想在 viewDidLoad 方法中添加到我的超级视图中。我使用以下代码执行此操作:

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

滚动视图旨在放置在视图的底部,它的高度应该是超级视图高度的一半。

我收到以下错误:

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) 

(“”、“”、“”、“”)

将尝试通过打破约束来恢复

请有人告诉我我做错了什么?谢谢你。

4

1 回答 1

2

rdelmar 解决了这个问题:

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

于 2013-03-25T18:39:07.737 回答