Skip to content

Instantly share code, notes, and snippets.

@valeriomazzeo
Last active February 25, 2016 15:57
Show Gist options
  • Save valeriomazzeo/6feeb07969edabba2326 to your computer and use it in GitHub Desktop.
Save valeriomazzeo/6feeb07969edabba2326 to your computer and use it in GitHub Desktop.
Dynamic TableHeaderView Height
- (void)viewDidLoad
{
[super viewDidLoad];
self.label = [[UILabel alloc] initWithFrame:CGRectZero];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
self.label.backgroundColor = [UIColor redColor];
self.label.textAlignment = NSTextAlignmentCenter;
self.label.numberOfLines = 0;
self.label.text = @"Valerio adjkashdjk asdjk asd adj aklsdjasl dkjasdkl askldj aklsdj aklsddj aklsdj aklsdjaklsdj aklsdj aklsd Tiziano.";
self.tableView.tableHeaderView = self.label;
// Simulate text change
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.label.text = @"Valerio";
[self.view setNeedsLayout];
});
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
CGSize size = [self.label systemLayoutSizeFittingSize:CGSizeMake(CGRectGetWidth(self.view.bounds), 0)];
// Avoid precision error
if (fabs(size.height - self.label.frame.size.height) > FLT_EPSILON) {
self.label.frame = CGRectMake(0, 0, 0, size.height);
self.tableView.tableHeaderView = nil;
self.tableView.tableHeaderView = self.label;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment