在view里动态添加控件,再代码添加约束后,约束不起作用!
我有一个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的大小。
请问怎么解决?