尝试添加简单约束但收到此错误消息 [英] Trying to add simple constraint but got this error message

查看:297
本文介绍了尝试添加简单约束但收到此错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着简单的实验只是为了尝试如何添加约束到一个对象。所以,我创建一个单独的UIView(黄色)垂直空间约束= 200从故事板像这样:

I'm trying simple experiment just to try how I can add constraints into an object. So, I create just a single UIView (yellow coloured) with vertical space constraint = 200 from storyboard like this :

这是我的介面档案:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *screenView;
@property (strong, nonatomic) IBOutlet UIView *container;

@end

并且我想要编程修改垂直空间约束<将此代码放在我的 viewDidLoad 上:code> _container :

and I want to programatically modify Vertical Space Constraint of _container by having this code on my viewDidLoad :

NSLayoutConstraint *myConstraint =[NSLayoutConstraint
                                   constraintWithItem:_screenView
                                   attribute:NSLayoutAttributeWidth
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:_container
                                   attribute:NSLayoutAttributeWidth
                                   multiplier:5
                                   constant:15];
[_container addConstraint: myConstraint];

但是当我运行它到模拟器,我得到这个错误消息:

but when I run it into simulator, I got this error message :

> 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

UPDATE : here's what I mean with _container

这里是_screenView

and here's _screenView

推荐答案

您的问题是_screenView是_containerView的superview,所以你的约束应该添加到它,而不是_containerView。另外,你说我想通过在我的viewDidLoad上使用这个代码来程序化地修改_container的垂直空间约束,但是你所做的是添加一个完全不同的约束(宽度约束),而不是修改它。如果要修改垂直间距约束,可以为其创建一个IBOutlet,并在代码中修改它的常量值。

Your problem is that _screenView is the superview of _containerView, so your constraint should be added to it, not _containerView. Also, you say, " I want to programatically modify Vertical Space Constraint of _container by having this code on my viewDidLoad", but what you're doing is adding a completely different constraint (a width constraint) instead of modifying the one you have. If you want to modify the vertical spacing constraint, you can make an IBOutlet to it, and modify it's constant value in code.

这篇关于尝试添加简单约束但收到此错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆