I have a UICollectionView in my app. I want it to constrain so it only has two cells on each horizontal row. For iPhone classes I achieve this by setting the width so three cells can't fit in the same horizontal row. This seems to be a fix but I don't know how to resize the cell for iPads. I have heard to use:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
but I can't access this function. Does Swift no longer support this function anymore?
Question: Can I force my screen to only display 2 cells on each horizontal row?
You should first conform to protocol UICollectionViewDelegateFlowLayout
and then implement the method :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width * 0.5, height: 30)
}
Related
The code to set the width and height of the collectionView's cell is as following:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/6, height: collectionView.frame.width/5)
}
When the Simulator is vertical before running the scheme, then we get
and
But when the Simulator is horizontal before running the scheme, we get
and
The size of cells will change as the Simulator's initial state. I think the reason is that collectionView.frame.width depends on the Simulator's state.
How to fix the cell's size using auto layout like the code before regardless the ipad's initial state?
Best and easiest way to get through this is to make it a sqaure rather than a Rectangle . basically have the same value to width/height in my opinion . If it doesnt help you try to below and one small change to your code is to use the bounds instead of the frames .
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width/5, height: collectionView.bounds.width/5)
}
also if you could guess a size margin in betweens you could also reduce that bounds.width/5 -10
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let standardWidth = max(collectionView.frame.width, collectionView.frame.height)
return CGSize(width: standardWidth/5, height: standardWidth/5)
}
This simple solution works well.
Trying to create a custom cell but when I am adding label in the cell collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) doesn't work.
Without label cell UI:
and the iPad simulator UI is:
But when I am adding label into the cell:
The iPad Simulator UI becomes :
In UICollectionViewDelegateFlowLayout in the method:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let emptySpace = 87*2 + 10
let remainingWidth = Int(self.view.frame.width) - emptySpace
return CGSize(width: remainingWidth/2, height: remainingWidth/2)
}
Are you building with Xcode 11 (GM 2)? I had similar issues implementing sizeForItemAt.
It seems something is causing the collection views to disregard the size returned in sizeForItemAt and instead use the size of the child views.
What worked for me is setting Estimate Size to None on the collection view in Interface Builder.
This is not ideal, especially if you have many cells, but it was the only fix I found unfortunately.
I want to set custom horizontal scroll for my CollectionView, that it will be scrolling by 1 cell (not by the whole width of my screen).
I could set HORIZONTAL scroll, but not custom. (See screens).
1 screen: my extension of my collectionView for UIScrollViewDelegate.
*I saw, that in console (see too) "x" - my offset = 290 - it's true! But on fact it is not 290. Paging was marked in "true" 2 screen: delegate and dataSource.
Help, please!
First you need to set your collectionView scroll direction horizontal (UICollectionViewScrollDirectionHorizontal) and set pagingEnabled to YES
It means your collection view will be scroll horizontally with one by one cell.
Set horizontal direction (Select collection view from XIB or Storyboard)
And for set pagination enable
myCollectionView.pagingEnabled = true
Updated :
You should use of UICollectionViewDelegateFlowLayout delegate
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let width = UIScreen.main.bounds.size.width
var height = 101.7 // set height as you want
return CGSize(width: width, height: height)
}
If using storyBoard the go to collection view and select the right handed tab "show the size inspector" and change
minimum spacing for cells = 0
minimum spacing for lines = 0
if you want to change directly from code then implement these UICollectionViewDelegateFlowLayout methods
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
must be remember scrolling direction set horizontal
and pagination enabled
and implement this delegate methods into code
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath:
IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height:
collectionView.frame.size.height)
}
}
You could create a custom UICollectionViewFlowLayout that will center the cells in the screen and then page one at a time. Karmadust have written a good blog post that I have successfully used in the past to do the same thing
http://blog.karmadust.com/centered-paging-with-preview-cells-on-uicollectionview/
I want to make multiple UICollectionViewCells of Various size like the image.
Use collectionViewFlowLayout and make a function inside collectionViewFlowLayout subclass and call that function from following function to give desired size for the cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
I want to display a collection view image which looks like so...
Here, as you can see the whole cell is of a rectangle shape with space for text and buttons below the image. I am not able to load such a 'rectangle' shaped cell in my collectioview. Any help would be appreciated.
Thanks in advance...:)
To set the size of item of a collectionview you can implement this UICollectionViewDelegateFlowLayout method
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 30, height: 100)
}
Your question is not very clear as it does not show your code or what result you got. But it is quiet simple
For customising cell size use below code and pass width and height accordingly
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: accordingly to your requirement, height: accordingly to your requirement)
}
Please check this link which can explain every bit in detail. And play with different value to get your required result.
Custom cell collectionview