我在自定义UITableViewCell中有三个视图:self.username, self.postText, self.containerView
其中self.containerView
是另外两个视图的超级视图。我希望它像这样布局:
Contrainer = self.containerView
-----------------------------
|.....5pt-margin............|
| [self.username] |
| [self.postText]* |
|.....5pt-margin............|
----------------------------
* Posttext can have a dynamic height - and the superview's height should adapt dynamically based on this height
我在我的单元格的init方法中尝试了以下NSLayoutConstraint:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[...]
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(self.username, self.postText, self.containerView);
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(5)-[self.username]-[self.postText]-(5)-|" options:0 metrics:nil views:viewsDictionary];
}
return self;
}
它目前崩溃,没有任何日志。正确的NSLayoutConstraint应该如何看?
答案 0 :(得分:2)
在constraintsWithVisualFormat:
中传递字符串参数时需要传递ASCII艺术视觉格式字符串,因此在您创建的@"V:|-(5)-[self.username]-[self.postText]-(5)-|"
格式self.username
中,self.postText
应该是id类型。