I have an application where I am using collectionview to display content which includes imageview, about 4 uilabels and a stackview which are all constrained to each other. This cell is scrollable vertically and not horizontally as I have to make provison for larger screen size like the iPad. The problem I have now is my CollectionViewFlowLayout height is static, How can I make it dynamic so that it resizes base on the height of the retuned content. the text of the label could vary so how do I handle this
below is how I have made my cell
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width, height: 400)
}
further code would be added on request and any help is appritiated
Related
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
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.
I have a UICollectionView with dynamic cell sizing which was working perfectly until I set proportional width constraints on the content inside the cell.
It seems now the UICollectionViewCell is sized to the size of the content, whereas actually I want the opposite - the content needs to be sized proportional to the cell size. Guess I'm missing something...
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = self.collectionView.frame.width
let height = self.view.frame.height/2.5
return CGSize(width: width, height: height)
}
For info, fixed by setting estimated size = none in IB
I am using
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 400, height: 600)
}
to specify my CollectionView's cell sizes. It works fine but as soon as I add a width constraint (UILabel.width == ContentView.width) to my UILabel inside the ContentView of the cell, the cell's width shrinks to the intrinsic width of the UILabel.
I am using UICollectionViewDelegateFlowLayout and horizontal scrolling.
How can I force the ContentView to stick to the cell size I specified and let the subviews follow the auto layout constraints?
The problem is that in the storyboard your collection view's Estimate Size is configured to Automatic:
You need to set it to None if you want your sizeForItemAt implementation to be obeyed rather than the internal constraints.
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.
}