Skip to content

Instantly share code, notes, and snippets.

@joe-goullaud
Forked from brentsimmons/headerview.m
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save joe-goullaud/11360905 to your computer and use it in GitHub Desktop.

Select an option

Save joe-goullaud/11360905 to your computer and use it in GitHub Desktop.
Tweak header view to use resizing mask as constraints and set directly as table view header view.
#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 = [[UILabel alloc] init];
_label.translatesAutoresizingMaskIntoConstraints = NO;
_label.text = @"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.numberOfLines = 0;
[_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];
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];
}
CGRect appFrame = [UIScreen mainScreen].applicationFrame;
VSSyncNoAccountHeaderView *headerViewContentView = [[VSSyncNoAccountHeaderView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(appFrame), 0.0)];
[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);
self.tableView.tableHeaderView = headerViewContentView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment