How to setup UICollectionViewCell margins correctly? - ios

I'm setting up a new prototype for a project, and I need to use a UICollectionView. The thing is, I'm struggling trying to set the correct margins to my UICollectionViewCell, but it doesn't work the way I want.
Here is my problem. I have a UICollectionView showing 3 cells. I'm showing only one cell in every "page". The orientation is horizontal and I'm using pagination as well.
The problem is the last cell. What I need to do to show the right margin of the last (third) cell.
Here's the image. The first one and second one are fine. The third is has a problem.
I'm subclassing the UICollectionViewCell. I don't thing that's the problem, though.
Here's the UICollectionView configuration and settings.
The UICollectionView is a 280x280 square. And the UICollecitonViewCell is a 260x260 square.
Here's the UICollectionViewCell configuration.
No extra code to change UICollectionView or UICollectionViewCell dimensions. I', trying to do the whole thing using Storyboard.
Any advice?
Thank!

Well.. After testing again, and again, and again I realised how UICollectionView and UICollectionViewCell work.
In my above set up, I make 2 big mistakes.
1) In the UICollectionViewCell setup, I set Custom size. I changed to default instead.
2) In the "Section Insets", I changed the right value to 10.
Here's the final result.
:)

Related

Multiline label in UIView vs UITableViewCell

I created a custom UITableViewCell with two labels and a button.
It behaved perfectly as expected, and looked like this:
Later, I realised I needed to use this particular view multiple places, one of which was not in a TableView. Therefore I decided to delete my cell, and create a custom UIView instead - and create an empty custom UITableViewCell only containing this custom view - constrained to 0 in all directions.
With the exact same elements and constraints, the UIView turned out like this when shown in a TableView as a cell:
Everything is identical. NumberOfLines is set to 0, all the constraints are the same.
I also observed something weird, that when I rotated the entire phone to landscape, it turned out like this:
This is exactly how I'd like it to look when in landscape.
If I now turn it back to portrait, suddenly it looks like this:
Now it suddenly looks exactly like I want it to look in portrait.. I just had to flip it to landscape and back to portrait.. Why is that?..
I played around with some variables, among them: preferredMaxLayoutWidth, and found that if I set this value to the size of the screen (minus the 16*2px padding) it looks as expected at first launch in portrait. However, I don't want to set this value. There is no magic number in px that will be right. The whole point of this is to get it fit properly on all screen sizes and orientations. I probably could re-set the preferred width every time superview's layoutSubviews or something is called, but I figured there has to be a solution for this..? Why did it work for UITableViewCell and not for UIView inside a UITableViewCell?
I have tried all sorts of sizeToFit etc., and don't find any questions or answers that targets this particular difference in UIView and UITableViewCell..
The problem here is a bit complex but I will try to explain it... When the view lays out, it initially does so independently of its container and then reports its height to the system when the system needs to determine the height of the cell, which happens before the cell is constructed. Once the cell exists and the device is rotated, the view "realizes" it's in a cell and constraints itself and reports its height appropriately.
Sadly, this is an issue in the table view system stemming from legacy issues with how cells were originally sized back before constraints existed.
The simplest solution is to put the labels and button back directly in the cell. Another option might be to make your external view a table view cell. You can instantiate a table view cell outside of a table view for that one spot it's needed in. Another option is to implement the table view delegate's tableView:heightForRowAtIndexPath: method and manually determine the size.
I am assuming that you are using following method for table view's height:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
So, Just add all your view's in stack and set top, bottom, leading and trailing constraints of UIStackView with respect to UIView of xib as:

Collection view Cell layout issue

I am designing an app that has the following layout. I was thinking of having the view as a collectionview.
A and B are a custom collection cells. How would I design the app so that the second and third rows have 2 and 3 cells as shown in the image?
I tried over-riding the sizeForItemAt indexPath method and then calculating the size of each cell based on the row it is in.
Not only it seems a little odd and but the cells don't align properly either.
I tried setting the estimatedItemSize property as well as over-riding preferredLayoutAttributesFitting by following this link.
Is there an easier way to do this UI ? Should I remove colleciton view and go with plain ol tableviews ?
Any help would be much appreciated.

Multiple cell types in UICollectionView

I had created two type of cells for my CollectionView, both are of different heights.
But when I run the app, the black cell is cut in half. I am unable to understand why. I tried changing the size of the cell, but it in turn makes the black cell over lap the cell below it. Please help with this.
Thanks
The size of the CollectionView item has to be changed dynamically. Kindly follow the following link to solve
UICollectionView - dynamic cell height?

UICollectionView that functions like a tableview

The designer came back with a layout that displays information differently.
Is it possible to make my UICollectionView function like a UITableView? Where each cell takes up one row and there is only one column?
Everything I found on the net seems to be about the opposite, making a UITableView be a UICollectionView
Actually, its very simple. Just make the width of the UICollectionViewCell equal to the width of the view.

Collectionview CustomLayout

I want a collectionview cell should be the dynamic height. I am getting data from server and loading in to tableview inside collectionview cell. That cell height should be change according to the table cell height, I am getting till here. But the problem is The cells are not aligning from the top. see this pic. The cell should create from top. This is demo code. Thanks
If I'm understanding your problem correctly, try setting self.yourCollectionView.automaticallyAdjustsScrollViewInsets = false
You will need to extend UICollectionViewLayout. And then in the prepareLayout method, you will need to specify the calculated frames of every item of your collectionview.
There are two tutorials:
http://www.skeuo.com/uicollectionview-custom-layout-tutorial is a very good tutorial for custom layout.
https://github.com/betzerra/MosaicLayout and https://www.cocoacontrols.com/controls/rfquiltlayout is pretty much what you want. A related question about rfquiltlayout on stackoverflow is RFQuiltLayout and UICollectionView

Resources