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.

2
  • awesome graphics, man. one upvote for you!
    – katzenhut
    Commented Apr 26, 2013 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? Commented Feb 13, 2014 at 6:13

2 Answers 2

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

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

7
  • But how would you maintain the button at the bottom of the screen when scrolling is not required? Commented Oct 14, 2012 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
    Commented Oct 14, 2012 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? Commented Oct 14, 2012 at 19:24
  • You can set the autoResizingMask of the view for doing this. Check the edit.
    – Rakesh
    Commented Oct 14, 2012 at 19:42
  • Adding a scrollView inside a scrollView that scroll in the same direction is asking for trouble
    – Paul.s
    Commented Oct 14, 2012 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.