Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anchor a UIButton to the bottom of a UITableViewController's view

I have the following requirement. When a UITableViewController's view is displayed, there are a variable number of rows. Underneath the rows, a button should be displayed.

When the number of rows is small, the button should be anchored to the bottom of the view.

When the number of rows is larger, the delete button should be placed immediately after the last row.

In other words:

enter image description here

And not:

enter image description here

My best attempt at this so far has involved setting a tableFooterView and trying to update its height using the contentSize of the UITableView, but I am running into all sorts of problems. I might continue down this path and ask for some help, but first I want to know if anyone has alternative (better) solutions.

The result must play nicely with a double-sized status bar (during a call for example) and I am targeting iOS 6.0. I am not using interface builder.

like image 641
Ben Packard Avatar asked Oct 14 '12 18:10

Ben Packard


1 Answers

One possible solution to achieve this effect might have to use two different solutions.

  1. If the amount of rows means that the button will be off the screen then use the footerView like you have been doing.

  2. If the amount of rows means that the button will not be off screen then

    1. Add the button to the tableView
    2. Implement - (void)scrollViewDidScroll:(UIScrollView *)scrollView and update the frame of the button to be offset from the bottom.

The offset from the bottom might follow some logic like this

  1. yOffset = CGRectGetHeight(tableView.frame) - (CGRectGetHeight(button.frame) + somePadding)
  2. yOffset += tableView.contentOffset.y

This would mean that the button still moves up and down with the scrolling but you don't have to mess with the footerView height

like image 146
Paul.s Avatar answered Sep 22 '22 13:09

Paul.s