1

I am creating a custom view such as

UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,0)];

and assigning it to table view's footer, like

self.tableView.tableFooterView = footerView;

I really dont have problem when i set it with frame but I dobt want to do that, I want to set it programmatically. So what i am doing is,

Now I have changed to

[self.tableView.tableFooterView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]|" options:0 metrics:nil views:dict]];

[self.tableView.tableFooterView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tableView]|" options:0 metrics:nil views:dict]];

but this is crashing my app, any help would be appreciated. Thanks.

Pankaj Gaikar
  • 2,357
  • 1
  • 23
  • 29

1 Answers1

1

You do not need to set the constraints for a table view's footer view relative to the table view. The positioning of the footer view is managed by the table view itself (though you can add constraints inside the footer view, i.e. footer view and its subviews). What you would want to return is the height of the footer view.

//Put this in your viewDidLoad
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)];
self.tableView.tableFooterView = footerView;

Advanced:

If your footer view resizes dynamically according to let's say a label or some other views in the footer view. Then you may subclass UIView for your footer view and override its intrinsic content size method to return the actual height of the footer view and then return self.tableView.footerView.intrinsicContentSize.height

Hope this helps you.

jarora
  • 5,384
  • 2
  • 34
  • 46
  • Thank you, that worked in case of only one section, but when I have as many sections as number of rows and I want to show footerview only at the end of the list, and also list will be dynamic, it does not work. Can you suggest any solution? thanks. – Pankaj Gaikar Jul 07 '15 at 08:18
  • Are you using fetchedResultsController? If not then you should use it. In that case you can just query [[[self fetchedResultsController] sections] count] == (section +1) (To check if this section is the last section). Then return height as 40 else return 0. – jarora Jul 07 '15 at 09:07
  • Downvoted; this answer is wrong. ```tableFooterView``` is not the same thing as a section footer (which uses ```heightForFooterInSection```). Section header/footers are the sticky views above and below a section while the tableFooterView/tableHeaderView are at the absolute top/bottom of a view and are not reused like cells/section views are. – Kevin Jul 31 '15 at 20:36