Skip to content

Instantly share code, notes, and snippets.

@johanforssell
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johanforssell/f754c1f1a9135f3baa3b to your computer and use it in GitHub Desktop.
Save johanforssell/f754c1f1a9135f3baa3b to your computer and use it in GitHub Desktop.
// in a UITableViewController (or any other view controller with a UITableView)
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, 0)]; // You might have to set height to 9999 or very high
header.translatesAutoresizingMaskIntoConstraints = NO;
// [add subviews and their constraints to header]
NSLayoutConstraint *headerWidthConstraint = [NSLayoutConstraint
constraintWithItem:header attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:size.width
];
[header addConstraint:headerWidthConstraint];
// Needed for iOS 7 >>>
[header setNeedsLayout];
[header layoutIfNeeded];
// <<<
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
[header removeConstraint:headerWidthConstraint];
header.frame = CGRectMake(0, 0, size.width, height);
header.translatesAutoresizingMaskIntoConstraints = YES;
self.tableView.tableHeaderView = header;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self viewWillTransitionToSize:self.tableView.bounds.size withTransitionCoordinator:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment