NSLayoutConstraint - 超级视图的动态高度

时间:2013-05-10 13:32:41

标签: ios objective-c cocoa-touch uitableview nslayoutconstraint

我在自定义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应该如何看?

1 个答案:

答案 0 :(得分:2)

constraintsWithVisualFormat:中传递字符串参数时需要传递ASCII艺术视觉格式字符串,因此在您创建的@"V:|-(5)-[self.username]-[self.postText]-(5)-|"格式self.username中,self.postText应该是id类型。