Skip to content

Instantly share code, notes, and snippets.

@fabiosoft
Last active July 9, 2021 09:47
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 fabiosoft/8bdf9c86c0cace234f7da24cde938a43 to your computer and use it in GitHub Desktop.
Save fabiosoft/8bdf9c86c0cace234f7da24cde938a43 to your computer and use it in GitHub Desktop.
Create a UITableView Footer / Header With a Dynamic Height (using AutoLayout). All snippets must be implemented by a UIVIewController subclass
- (void)viewDidLoad {
[super viewDidLoad];
.
.
.
FooterView *footerView = [[FooterView alloc]init...];
footerView.delegate = self;
CGFloat footerHeight = 200.0;
footerView.frame = CGRectMake(0, 0, self.view.frame.size.width, footerHeight); //dummy, but mandatory
self.tableView.tableFooterView = footerView;
}
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
if (!self.tableView.tableFooterView){
return;
}
UIView *footerView = (UIView /*you can replace with you concrete type*/ *)self.tableView.tableFooterView;
CGFloat width = self.tableView.bounds.size.width;
CGSize size = [footerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
if (footerView.frame.size.height != size.height) {
footerView.frame = CGRectMake(0, 0, width, size.height);
self.tableView.tableFooterView = footerView;
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard let footerView = self.tableView.tableFooterView else {
return
}
let footerView = self.tableView.tableFooterView
let width = self.tableView.bounds.size.width
let size = footerView.systemLayoutSizeFitting(CGSize(width: width, height: UIView.layoutFittingCompressedSize.height))
if footerView.frame.size.height != size.height { //avoid hang
footerView.frame.size.height = size.height
self.tableView.tableFooterView = footerView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment