Horizontal collection view with different height of cells - swift

I am trying to do some horizontal collection view with dynamic height of each cell. Like citymapper does when you have aready choosen a trip. ¿Has anyone got any idea of how to do this? Even though if it's not with a collection view.
Video of the behaviour

One way of doing this is implementing the UICollectionViewDelegateFlowLayout of the collectionView.
One of the methods of this delegate is final public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize. Here you can return the size of the collectionCell depending on the content or whatever you use to determinate the height.
Hope it helps.

Related

UICollectionView - How to add "PAGING" in UICollectionView scrolling with vertically

I have implemented UICollectionView with the vertical scrolling and paging, first image showing properly but after scrolling layout get changed.
Code -
// MARK: - UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}
I enable the paging from UICollectionView
Also attached images below.
Thanks in advance.
pls check below screen sort setting of collectionview into storyboard.
Estimate size none
others min spacing for cells & for lines are zero or not

Issues with resizing my UICollectionViewCell

I am currently trying to resize my cell within my CollectionView. I have already implemented the dataSource, CollectionViewDelegate and CollectionViewDelegateFlowLayout.
This is the code I have for the latter:
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout, sizeForItemAt
indexPath: IndexPath) -> CGSize
{
return CGSize(width: 100.0, height: 100.0)
}
}
The problem is that when I head over to my storyboard and select my CollectionView and then go to the Attributes Inspector to switch "Estimate Size" from "Automatic" to "None," I would get three columns after running my app [Image #1].
The result I am looking for is to be able to have one middle column with a bigger cell as I show on Image #2.
Has anybody dealt with this problem before?
In order to show a collection view cells as a list, you can change your cell width equals to the width of the collection view. Giving constant width will not work for different device size.
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout, sizeForItemAt
indexPath: IndexPath) -> CGSize
{
let width = collectionView.frame.width
return CGSize(width: width, height: 100.0)
}
}
And give your desired constraint values for the inner rectangular view to achieve the desired look. Make collectionView cell background to clear.

How to make 2 rows in horizontal collection view without using sections

The horizontal scrolling in UICollectionView only returns one row and I need to make a horizontal scrolling collection view with 2 rows just like the image below
[1]: https://i.stack.imgur.com/Vs1kt.png :[1]
You need to set fix height of the CollectionView and then use sizeForItemAt() under UICollectionViewDelegateFlowLayout which returns CGSize. You have to manage the cell height something like this.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 50.0, height: collectionViewHeight / 2) //<-- Manage height and width accordingly.
}

Fitting all items into one collection view row

Is there a way to make all items in a collection view fit in one row and not automatically place items in an another row? I'm trying to copy something like Instagram's story display like the link below:
However, In my collection view, I'm getting a count from a server, and displaying the information in a collection view but it always goes to the next line after 3 items are displayed.
// number of sections function
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pictures.count
} // end of function
I think you don't want Vertical scrolling in Collection view.
For horizontal scrolling in collection view, select collection view in storyboard and change scroll direction to horizontal.
Refer this below pic:
you can add collectionViewLayout delegate method like this. CGFloat(3.0) is how many item you want, you have to divide it from collectionView's width
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemWidth: CGFloat = ((collectionView.bounds.size.width) / CGFloat(3.0))
return CGSize(width: itemWidth, height: collectionView.bounds.size.height)
}

Easy UICollectionView Custom Layout

I want to create a custom layout which should look like the picture here: Custom Layout
I found a tutorial by Ray Wenderlich (https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest) which does more or less the same but it was to complicated for me so I wasn't able to shift this to my needs.
I just need the easy layout, and I also know the size for my cells, I don't have to calculate them.
Could anyone please help me.
In
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
of UICollectionViewDataSource create a switch for the variable indexPath.row and set the size of the cell:
cell.frame = CGMake(height: myHeight; width: myWidth)