Created
April 26, 2014 22:09
-
-
Save brentsimmons/11332478 to your computer and use it in GitHub Desktop.
UITableView header issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@brentsimmons Did you ever come up with a solution to this? I am currently trying to subclass UITableViewHeaderFooterView in order to display my own multi-line UILabel. It's just a mess, spent several hours with AutoLayout and Reveal debugging this.. UITableViewAutomaticDimension should work, the contentView has the correct size. But instead it sits on top of the 1st section cell which it overlaps.