I have created a very simple test case to reproduce this issue.
I am trying to set a footer view programmatically to a tableview. Please note that I am referring to the
Dynamic-sized UITableView
Header and Footer views do not always play nice with auto-layout, so you need to give it a little help.
Here is an example that creates a simple UIView
for the footer view and adds an "expanding" UILabel
(number of lines set to Zero). The footer view is created with an explicit CGRect for its frame, and the label is pinned to all four sides with auto-layout constraints.
In viewDidLayoutSubviews()
, we tell auto-layout to calculate the frame of the footer view, based on the constraints on its contents, and then we update the frame values (well, specifically the height).
//
// this assumes IBOutlet has been set for "theTableView"
//
- (void)viewDidLoad {
[super viewDidLoad];
// standard stuff
[_theTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"simpleCell"];
_theTableView.delegate = self;
_theTableView.dataSource = self;
// instantiate a view for the table footer
// width doesn't matter (it will be stretched to fit the table by default)
// set height to a big number to avoid a "will attempt to break constraint" warning
UIView *footerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1000)];
// give it a color so we can see it
footerContainer.backgroundColor=[UIColor greenColor];
// set the footer view
_theTableView.tableFooterView = footerContainer;
// instantiate a label to add to the footer view
UILabel *aLabel = [UILabel new];
// auto-sizing the height, so set lines to zero
aLabel.numberOfLines = 0;
// give it a color so we can see it
aLabel.backgroundColor = [UIColor yellowColor];
// set the text to 8 lines for demonstration purposes
aLabel.text = @"Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8";
// standard, for auto-sizing
aLabel.translatesAutoresizingMaskIntoConstraints = NO;
// add the label to the footer view
[footerContainer addSubview:aLabel];
// constraint the label to 8-pts from each edge...
[aLabel.topAnchor constraintEqualToAnchor:footerContainer.topAnchor constant:8.0].active = YES;
[aLabel.leftAnchor constraintEqualToAnchor:footerContainer.leftAnchor constant:8.0].active = YES;
[aLabel.rightAnchor constraintEqualToAnchor:footerContainer.rightAnchor constant:-8.0].active = YES;
[aLabel.bottomAnchor constraintEqualToAnchor:footerContainer.bottomAnchor constant:-8.0].active = YES;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// get a reference to the table's footer view
UIView *currentFooterView = [_theTableView tableFooterView];
// if it's a valid reference (the table *does* have a footer view)
if (currentFooterView) {
// tell auto-layout to calculate the size based on the footer view's content
CGFloat newHeight = [currentFooterView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// get the current frame of the footer view
CGRect currentFrame = currentFooterView.frame;
// we only want to do this when necessary (otherwise we risk infinite recursion)
// so... if the calculated height is not the same as the current height
if (newHeight != currentFrame.size.height) {
// use the new (calculated) height
currentFrame.size.height = newHeight;
currentFooterView.frame = currentFrame;
}
}
}
This can also be helpful when trying to get auto-sizing table view header views to work properly.