Cocoa touch UIView基于自动布局的内容的高度
我正试图在UITableview中创建一个包含3个标签的节标题,其布局有点像这样Cocoa touch UIView基于自动布局的内容的高度,cocoa-touch,ios6,autolayout,ios7,Cocoa Touch,Ios6,Autolayout,Ios7,我正试图在UITableview中创建一个包含3个标签的节标题,其布局有点像这样 ------------------------------- | title | dist | | | | Description | | | ------------------------------- 标题和时间都是固定大小,但描述需
-------------------------------
| title | dist |
| |
| Description |
| |
-------------------------------
标题和时间都是固定大小,但描述需要根据内容自动调整其高度,最小高度为一行文本
我当前的标题结构包括
UIView
|_ UIView
|_ UILabel
|_ UILabel
|_ UILabel
外部UIView是具有清晰背景的节标题本身,内部UIView是带有标签的可见视图
我要做的是,让节标题根据描述标签中的内容调整其高度
我尝试过实现tableView:heightForHeaderInSection:但它似乎不起作用。iOS抱怨必须打破约束,我想这是有道理的,因为我已经设置了约束,但然后用这种方法手动覆盖它们
如何创建允许视图自行调整大小的约束?现在,我有了ViewForHeaderSection的以下实现:
任何帮助都会很好:您找到解决方案了吗?没有:仍然没有。我可以暂时不用它,但那只是为了演示应用程序,所以我没有足够的内容来破坏它。但仍在寻找理想的解决方案
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
UIView *header = [UIView new];
UIView *headerBg = [UIView new];
UILabel *title = [UILabel new];
UILabel *distance = [UILabel new];
UILabel *desc = [UILabel new];
headerBg.translatesAutoresizingMaskIntoConstraints = NO;
title.translatesAutoresizingMaskIntoConstraints = NO;
distance.translatesAutoresizingMaskIntoConstraints = NO;
desc.translatesAutoresizingMaskIntoConstraints = NO;
[headerBg setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:0.8]];
[title setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0]];
[title setTextColor:kFONT_COLOUR];
NSString *address = _place.address ? (_place.address.length > 0 ? _place.address : @"Unknown Address") : @"Unknown Address";
[title setText:address];
[distance setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0]];
[distance setTextColor:kFONT_COLOUR];
[distance setText:@"Distance: ?m"];
[distance setTextAlignment:NSTextAlignmentRight];
[desc setFont:[UIFont fontWithName:@"HelveticaNeue" size:11.0]];
[desc setTextColor:kFONT_COLOUR];
[desc setNumberOfLines:0];
[desc setLineBreakMode:NSLineBreakByWordWrapping];
[desc setText:_place.desc ? (_place.desc.length > 0 ? _place.desc : @"No Description Given") : @"No Description Given"];
[headerBg addSubview:title];
[headerBg addSubview:distance];
[headerBg addSubview:desc];
NSDictionary *bindings = NSDictionaryOfVariableBindings(title, distance, desc, headerBg);
[headerBg addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-9-[title]-[distance(100)]-9-|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:bindings]];
[headerBg addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-9-[desc]-9-|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:bindings]];
[headerBg addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-9-[title(20)]-[desc]-|" options:NSLayoutFormatAlignAllLeading metrics:nil views:bindings]];
[header addSubview:headerBg];
[header addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[headerBg]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:bindings]];
[header addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerBg]-9-|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:bindings]];
return header;
}
return nil;
}