0

Requirements:
I have a table and a button I need to place on my screen. The table can have zero to many rows. The button needs to be at least 40 pts below the bottom of the table content, and 83 pts above the bottom of the screen. So when there are only a few rows, the button is far away from the last row of the table, but when there are many rows, the button may not appear on the screen until the user scrolls down the table.

I have considered using a UITableView footer but it would always place the button right after the last row of the table content.

Right now I am considering using a UIScrollView and placing the table and button in a view that grows as the table content grows. The table height constraint would grow as well. I would use inequality constraint on button so it's always at least 40 pts below bottom of table and 83 pts above bottom of the view.

Is there a better way though? What I described seems a little complicated.

UI design of when the table is short:
enter image description here

I do not have a pic for when the table is long and scrolls off the screen, but button has spacing between last row and bottom of scrolled view

jcharch
  • 150
  • 16
  • You can use `FooterView` for your `UITableView`. – emrcftci Jul 10 '19 at 14:20
  • @EmreCiftci FooterView would place the button immediately after the last row of my table regardless of number of rows, right? – jcharch Jul 10 '19 at 14:26
  • Using inequality constraint is better way. but please also share screen shot of viewcontroller after selecting uitableview. so that we can get to specific problem u having. – MRizwan33 Jul 10 '19 at 14:27
  • @MRizwan33 Updated. Hope it clarifies what I'm trying to accomplish – jcharch Jul 10 '19 at 14:35
  • you want your button stop at buttom of screen if there is scrolling in tableview. if there is no scrolling in tableview then you want your button position right bottom of tableview? – MRizwan33 Jul 10 '19 at 14:57
  • @MRizwan33 No, if the table grows long enough it should push the button off screen. The user needs to scroll down to access it. But if the table is short then the button should be positioned as in the screenshot. – jcharch Jul 10 '19 at 14:58
  • @jcharch - Here is one approach: https://stackoverflow.com/a/56134043/6257435 ... in that example, just replace the "Footer View" (which is a normal `UIView`) with your button. – DonMag Jul 10 '19 at 15:02
  • then add footer view as example given in below answers. And please don't forget to upvote the answers you seems helping you. – MRizwan33 Jul 11 '19 at 06:29

2 Answers2

0

This can be achieved by the help of these 3 constraints :-

  1. Set tableView height constraint (Constraint1). Adjust this constraint value according to the tableView content. If the cell height is constant, then this constraint’s value will be (cell count * cell height)
  2. Set constraint from tableView bottom to button top (Constraint2). The value of this will be >= 40 and priority equals to Constraint1, the default 1000.
  3. Set constraint from superview bottom to button bottom (Constraint3). The value of this will be 83 and its priority should be set as lesser than Constraint1 and Constraint2 (like 800 or something)

So when the table view content is less, Constraint3’s 83 padding from bottom will be considered, since Constraint2 is ready to elongate further from 40. But when the content increases, when there is clash between Constraint2 and Constraint3, the latter will not be considered because it has lesser priority.

Vincent Joy
  • 2,598
  • 15
  • 25
  • In a scrollview? @vincent-joy – jcharch Jul 10 '19 at 14:39
  • No you don't have to use scroll view. Just a view controller with a table view and button inside it. – Vincent Joy Jul 10 '19 at 14:43
  • Oh I thought to scroll I need a scroll view. So if the table grows long enough to push the button off screen I'll be able to scroll down to get to it? I'll try it out. – jcharch Jul 10 '19 at 14:56
  • Oh yeah I think you are correct. Actually I didn't consider that scenario. Please try putting the table view and button in scroll view as well. – Vincent Joy Jul 10 '19 at 16:07
0

Another solution could be to add a footer table view with your custom content. Any tableview has a header and a footer view and could be designed as you prefer.

enter image description here

enter image description here

In my case (just for example) i set 2 UIViews with a label inside (the color is just to show you the view), you can replace the label with a button and change the background color. To show it or not you can dinamically change the footer height or maybe hide or show the view, depending on your purpose.

Hope this help :)

Alessandro
  • 2,927
  • 1
  • 10
  • 14