Scrolling / static UITableView footer - ios

I want to have a UITableViewCell with a behaviour mixed between a section footer and the table footer:
If the table is not filled (meaning there are not enough cells to fill the entire screen), the cell should stay at the bottom of the table, anchored to the screen edge, leaving blank space between itself and those above. (behaving like a table footer)
If the table is filled, the cell should start scrolling and always remain at the bottom of the table. (behaving like a section footer)
I'd like to avoid as much as possible weird tricks to achieve this, is there an elegant solution that allows me to do this?

Make your footer a separate view on top of the tableView with a constraint to the bottom of the tableView and take an outlet to this constraint. Override scrollViewDidScroll and get the bottom y coordinate of the last visible cell by using tableView.visible cells and calling CGRect.maxy on its frame (if there is no last cell your constraint constant is tableView.frame.size.height - footerView.frames.size.height). Take the difference of the tableView.frame.maxY and the last visible cell's maxY. If the cell is past the tableView.frame.maxY - footerView.frames.size.height then you set your constraint constant to 0, otherwise you set it to the difference.
This has the effect of pinning your footer view to the last visible cell, unless this would force the footer past the bottom of the table, in which case you just pin it to the bottom of the table instead. If there is no last cell you pin the footer to the top of the table.

Related

How should I place a button under UITableView and have it move down as the table grows?

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:
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
This can be achieved by the help of these 3 constraints :-
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)
Set constraint from tableView bottom to button top (Constraint2). The value of this will be >= 40 and priority equals to Constraint1, the default 1000.
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.
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.
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 :)

Pin UICollectionViewCell to top of UICollectionView row

Is there any way to vertically align a cell within a row? My use case is pinning a cell to the top of the row. The cells I have in the row have variable height and right now the smaller ones end up in the center of the row while the larger one consumes the row height. I'd like the small ones to be pinned to the top so that they are flush to the top.
Here is an example of variable sized cells; however, these are pinned to the bottom whereas I want to pin mine to the top.
Source: https://developer.apple.com/reference/uikit/uicollectionview

Force UICollectionView to scroll until a specific cell is centered

First of all, I already read How to force UICollectionView with fewer items to scroll? and it did not solve my problem.
I have an horizontal UICollectionView with 5 cells, and only 5 cells are needed to fill the screen width. This means the 3rd cell is centered. How can I scroll so the 4th cell is centered?
When using scrollToItemAtIndexPath, it won't scroll since there is not enough cells to scroll. In other words, if I want my 4th cell to be centered, there would be a blank space at the end since I only have 5 cells (and vice-versa for the 2nd cell).
Is adding empty cells the only solution?
You could adjust the insets on the collection view. Add enough space that you can center the last cell.
More info from Apple

Unable to move cell up in uicollection view from storyboard?

I have added UICollectionView.I have also added the Cell in the UICollectionView.The issue is cell always getting space from top of 74.On interface builder the y space column always shows the disabled value please tell how can i remove such issue?
Any given cell's position is determined by the collection view. You can adjust the cell's insets to increase spacing between cells and the scroll view's insets to position all of the cells in the scroll view, you can't directly manipulate a cell's position out of the box. When noting the position of your first cell you should also take into account whether your viewController has extend edges under top bars on and whether it has adjust scroll view insets on.

How to find the height of the tableView left over by the cells?

I have a tableview where some gap is left after the cells are over.
I would like the table's footer to take that space.
How do I accomplish this ?
The footer of a table is automatically displayed after the last of the cells. If you want to make sure that the footer fills all possible space from the last cell to the bottom of the view then just make the footer the height of the screen. If you want to dynamically arrange any subviews in your footer so that they are all visible then you probably need to add up the height of all your cells to determine how much of the footer is visible.

Resources