Pin UICollectionViewCell to top of UICollectionView row - ios

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

Related

Autolayout equal spacing and weighted spacing

I'm trying to use autolayout to create a layout for cells in my table view. I will attach a screenshot of what I am trying to achieve.
The main rules are that the button at the bottom must be the width of the cell. Then the image on the left should sit tight to the left but exactly halfway between the top of the entire view and the top of the bottom button. Then I need the 4 rows on the left to sit equally between top of the view and top of the button. And for them to display in a list like they do, and with a small image and text label in line.
I have tried quite a few ways to get this to work. I have managed to get the button to stay at the bottom as required. However, I am struggling to get the imageview to stay as a square on the left and then the 4 rows on the right to take up the rest of the space.
Any help would be appreciated.
You can achieve what you want by wrapping imageView and rows into separate container views (see
Attachment)
Steps for imageView:
ContainerView1 constrained to the top of the cell view and to the top of Check In button
imageView constrained vertically to the center of the ContainerView1. Also set imageView leading constraint to the ContainerView1 leading as you need
Steps for rows
Wrap rows into ContainerView2. ContainerView2 constrained to the top of the cell view and to the top of Check In button.
Constrain firstRow top to the top of ContainerView2. Each subsequent row should be constrained by its top the previous row's bottom. Constrain lastRow bottom to the ContainerView2 bottom
For all rows except firstRow drag "equal height" constraint to be equal to the firstRow height
That is all!
P.S. if your application works starting with iOS 9.0 you can use UIStackView instead of manual setting constraints in your cell
To keep the images square you need a 1:1 aspect constraint.
Hard to know what else is going wrong with your layout without seeing your constraints but for reference this is how I'd lay out this view.

Scrolling / static UITableView footer

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.

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

UICollectionView cells, auto layout and collectionview layout changes

I have a collection view which displays cells looking like a classic table view with image, title and subtitle.
I want to be able to switch the layout to a grid showing only the images, 3 in a row.
The content of the collection view cell is layouted in a storyboard with auto layout. The imageView has the following constraints:
Leading 0 to the left cell edge
Top 0 to the top cell edge
Bottom 0 to the bottom cell edge
Fixed width
Horizontal space to the labels
After the layout change the imageView should have constraints of zero to all edges of the container, so that it fills it completely.
At the moment when I change the layout the app crashes with Unable to simultaneously satisfy constraints.. What is the best way to fix the constraints when the layout is changed, maybe even replace the cell class?
Turns out the problem was in some other part of the code.
You can change the collectionView layout with setCollectionViewLayout:animated:completion: and reload the visible cells in the completion block.

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