I'm adding a UIButton to a tableView footer programmatically. This button has a left and right margin that is equal to the tableView margin:
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteButton.frame = CGRectMake(10, 60, 300, 34);
deleteButton.autoresizingMask = UIViewAutoresizingFlexibleWidth
I'm adding autoresizingMask because I want to support rotation. However, it does not work as I want, as the button stretches all the way down to the right, as shown by the image below.
Any idea how to fix it? If I remove the autosizing property then the margin is correct.
UITableView
resizes the view (and all its subviews) returned by- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
. To fix your issue, you need a wrapper view that layouts yourUIButton
without using autoresizing. Here’s an example implementation of such anUIView
class:Simply return this view in
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
and your button should be displayed properly.I’ve also uploaded a sample project which completely implements the above solution.