tableview标题中带有autolayout的ios8不再有效

时间:2014-09-26 01:29:26

标签: ios objective-c iphone ios8

我以前能够在我分配给self.tableView.tableHeaderView的视图的子视图中使用AutoLayout,但看起来这在iOS 8中不再有效。

self.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.view.frame.size.width, 200.f)];

self.questLabel = [[CLAPaddedLabel alloc] initForAutoLayout];
self.questLabel.text = @"My label here";
self.questLabel.textAlignment = NSTextAlignmentCenter;
self.questLabel.textColor = [UIColor blackColor];
self.questLabel.font = [UIFont fontWithName:kCLAfontIMFellItalic size:19];
[self.tableHeaderView addSubview:self.questLabel];

self.tableView.tableHeaderView = self.tableHeaderView;

我的自动布局规则在updateViewConstraints中设置,不受尊重。控制台显示没有错误。

1 个答案:

答案 0 :(得分:0)

您可能想尝试在标题视图上设置宽度和高度的约束,这从未给我带来任何问题:

 self.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero()];

 //do your config for the labels on the header view

 self.tableView.tableHeaderView = self.tableHeaderView;
 [self.tableHeaderView setTranslatesAutoresizingMaskIntoConstraints: NO];
 [self.tableHeaderView addConstraint:[NSLayoutConstraint constraintWithItem: header attribute:NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant: self.view.frame.size.width]];
 [self.tableHeaderView addConstraint:[NSLayoutConstraint constraintWithItem: header attribute:NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant: 200.0f]];
 [self.tableHeaderView setNeedsLayout];
 [self.tableHeaderView layoutIfNeeded];