在view里动态添加控件,再代码添加约束后,约束不起作用!

工程师123123123 2015-11-05 02:52:49
我有一个view,名叫_takeview,里面动态添加一个控件_commbox,

先定义commbox位置,添加到take view里成为其子视图,最后添加约束,

代码如下:

_commbox = [[Commbox alloc] initWithFrame:CGRectMake(0, 0,self.takeview.frame.size.width, self.takeview.frame.size.height)];

[self.takeview addSubview:_commbox];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.commbox attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqualtoItem:self.takeview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.commbox attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqualtoItem:self.takeview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.commbox attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqualtoItem:self.takeview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.commbox attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqualtoItem:self.takeview attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];



takeview的约束我是在故事版里定义的,takeview的tailing约束是距离整个屏幕的tailingmargin有一定距离的。我是希望commbox和takeview一样大小的,不要超出其范围。

这段代码是在viewcontroller的viewdidload里面动态添加的,运行后发现,commbox的tailing超出了整个屏幕的外面,也就是说commbox的大小长宽,超出了takeview.frame.size的大小。

请问怎么解决?
...全文
241 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
工程师123123123 2015-11-06
  • 打赏
  • 举报
回复
现在报错内容为 2015-11-06 11:36:02.587 UneditableCommBox[5631:1730151] 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) ( "<NSIBPrototypingLayoutConstraint:0x14667160 'IB auto generated at build time for view with fixed frame' H:|-(70)-[UIView:0x14565840](LTR) (Names: '|':UIView:0x146506b0 )>", "<NSLayoutConstraint:0x1454ab60 H:|-(30)-[UIView:0x14565840] (Names: '|':UIView:0x146506b0 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x1454ab60 H:|-(30)-[UIView:0x14565840] (Names: '|':UIView:0x146506b0 )> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
工程师123123123 2015-11-06
  • 打赏
  • 举报
回复
引用 1 楼 jimmy808126 的回复:
commbox.translatesAutoresizingMakeIntoConstraints = NO; 这句调用下
没什么变化啊,是不是故事版里的约束与代码约束之间产生了矛盾,或者是有了代码约束,故事版约束就没有发挥作用了。
jimmy808126 2015-11-05
  • 打赏
  • 举报
回复
commbox.translatesAutoresizingMakeIntoConstraints = NO; 这句调用下

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧