9

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.

Ben Packard
  • 26,102
  • 25
  • 102
  • 183
  • awesome graphics, man. one upvote for you! – katzenhut Apr 26 '13 at 13:54
  • Hey @Ben I asked for a similar problem. You might want to check out stackoverflow.com/questions/21692880/uiscrollview-with-sticky-footer-uiview-and-dynamic-height-content . How did you solved this? – Gaston Morixe Feb 13 '14 at 06:13

2 Answers2

1

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

Paul.s
  • 38,494
  • 5
  • 70
  • 88
0

Keep both the table view and a button inside scroll view. Keep the button at the bottom of the scrollview. For proper scrolling to work you might want to set the scrollEnabled property of the scroll views. For more details on that check this up

Scrolling a UITableView inside a UIScrollView

EDIT:

yourView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin ;

Check the accepted answer for this question for more details on implimenting struts and springs using code: UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C

Community
  • 1
  • 1
Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • But how would you maintain the button at the bottom of the screen when scrolling is not required? – Ben Packard Oct 14 '12 at 19:15
  • By keeping the button attached to the bottom of the scrollView in the size inspector. Even when the size of your uitableviewcell changes the minimum size of the scrollview always remains the same. – Rakesh Oct 14 '12 at 19:17
  • I should have mentioned in the question that I am not using interface builder (I will update it now). Can you explain the API that IB uses to 'keep the button attached to the bottom of the scrollView' so I can attempt to replicate it in code? – Ben Packard Oct 14 '12 at 19:24
  • You can set the autoResizingMask of the view for doing this. Check the edit. – Rakesh Oct 14 '12 at 19:42
  • Adding a scrollView inside a scrollView that scroll in the same direction is asking for trouble – Paul.s Oct 14 '12 at 19:46
  • I understand we will to have resolve scrolling issues in child and parent views when nesting scrollviews. But how else can we implement such a requirement? – Rakesh Oct 14 '12 at 20:08
  • @Rakesh well it's non standard - which normally means more awkward to do - see my answer for a possible solution – Paul.s Oct 14 '12 at 20:15