Skip to content

Instantly share code, notes, and snippets.

@brentsimmons
Created April 26, 2014 22:09
Show Gist options
  • Save brentsimmons/11332478 to your computer and use it in GitHub Desktop.
Save brentsimmons/11332478 to your computer and use it in GitHub Desktop.
UITableView header issues
#pragma mark - Class Methods
+ (BOOL)requiresConstraintBasedLayout {
return YES;
}
#pragma mark - Init
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cloud-logo"]];
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
_imageView.contentMode = UIViewContentModeRedraw;
[_imageView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[_imageView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self addSubview:_imageView];
_label = [VSSyncUI descriptionLabel];
_label.translatesAutoresizingMaskIntoConstraints = NO;
_label.text = NSLocalizedString(@"Vesper automatically keeps your notes backed up and shared across your devices. Sign in or create an account to get started. We’ll take care of the rest.", @"");
_label.contentMode = UIViewContentModeRedraw;
[_label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[_label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[self addSubview:_label];
[self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
self.backgroundColor = [UIColor redColor];
self.translatesAutoresizingMaskIntoConstraints = NO;
return self;
}
#pragma mark - API
- (void)updateLabelWidth:(CGFloat)superviewWidth {
self.label.preferredMaxLayoutWidth = superviewWidth - (20.0f * 2.0f);
}
#pragma mark - Constraints
- (void)updateConstraints {
UIView *imageView = self.imageView;
UIView *label = self.label;
NSDictionary *bindings = NSDictionaryOfVariableBindings(imageView, label);
NSLayoutConstraint *constraintCenterX = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[self addConstraints:@[constraintCenterX]];
NSArray *labelHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[label]-20-|" options:0 metrics:nil views:bindings];
[self addConstraints:labelHorizontalConstraints];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[imageView]-20-[label]-20-|" options:0 metrics:nil views:bindings];
[self addConstraints:verticalConstraints];
[super updateConstraints];
}
UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:nil];
CGRect appFrame = [UIScreen mainScreen].applicationFrame;
VSSyncNoAccountHeaderView *headerViewContentView = [[VSSyncNoAccountHeaderView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(appFrame), 400.0)];
headerViewContentView.translatesAutoresizingMaskIntoConstraints = NO;
[headerViewContentView updateLabelWidth:CGRectGetWidth(appFrame)];
[headerViewContentView setNeedsUpdateConstraints];
[headerViewContentView updateConstraintsIfNeeded];
// CGSize bestSize = [headerViewContentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
//// bestSize.height += 20.0;
// headerViewContentView.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(appFrame), bestSize.height);
[headerView.contentView addSubview:headerViewContentView];
self.tableView.tableHeaderView = headerView;
@InfiniteCode
Copy link

@winkelsdorf Were you able to solve this on your side?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment