我正在尝试简单的实验,只是为了尝试如何将约束添加到对象中。因此,我从情节提要中只创建了一个具有垂直空间约束 = 200 的 UIView(黄色),如下所示:
这是我的接口文件:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *screenView;
@property (strong, nonatomic) IBOutlet UIView *container;
@end
我想_container
通过在我的代码上以编程方式修改垂直空间约束viewDidLoad
:
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:_screenView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:_container
attribute:NSLayoutAttributeWidth
multiplier:5
constant:15];
[_container addConstraint: myConstraint];
但是当我在模拟器中运行它时,我收到了这个错误消息:
> 2013-09-19 05:48:26.488 container[6036:c07] *** Terminating app due to
> uncaught exception 'NSGenericException', reason: 'Unable to install
> constraint on view. Does the constraint reference something from
> outside the subtree of the view? That's illegal.
> constraint:<NSLayoutConstraint:0x7168860 UIView:0x7168b50.width ==
> 5*UIView:0x7169360.width + 15> view:<UIView: 0x7169360; frame = (0 0;
> 0 0); autoresize = TM+BM; layer = <CALayer: 0x7168bb0>>'
我做错了什么?谢谢你...
更新:这就是我对 _container 的意思
这是 _screenView