I have an collectionview with various cells but I have a problem:
if I execute code on iPhone 6s simulator I get this (I want this on every device):
if I execute it on iPad Retina I get this:
and if I execute it on iPhone 5 (I dont see cells):
How do I solve this problem?
Can you help me?
//UPDATE:
if I remove all object in uicollectionviewcell (in this case only the image view) everything work fine (the cells are the same on iPad, iPhone 5 I see the cell) but naturally in the cell I havent objects
Have you tried implementing sizeForItemAtIndexPath to give a width equal to the collection view's width?
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width, height: /* your height */)
}
Related
I have a UICollectionView with a custom cell, however, I am trying to impose constraints on the UICollectionView, so it is always half the width of the screen and half the height of the screen. The one issue I continually encounter is that while the UICollectionView changes according to what device the app is deployed on, the UICollectionViewCells hold the exact same pixel width and height dimensions. This is the code I have tried to implement to make each cell the same height and width as the CollectionView.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = collectionView.frame.height
let width = collectionView.frame.width
return CGSize(width: width, height: height)
}
This code, however, doesn't work as the size of the cells are simply the values that are specified in the size inspector for the UICollectionView.
I'm unable to figure out what I am doing wrong here with my code. Thank you very much in advance for your help.
The issue is your collection view cell size is fixed always. So the cell size is not adjust according to screen size.Use view frame size.(remove values from size inspector)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
I am new to iOS app development and I am trying to understand what is the issue in iPhone 11 Pro Simulator of my layout when works well in iPhone 8 Plus.
Please find the screenshot below:
Can anyone help me out on why this is happening?
If you require anymore information on the code, Please let me know.
EDIT:
This is my CollectionView Attributes:
It appears that the cell is little zoomed in when it comes to the iPhone 11 Simulator.
You must have to implement Size for item method and need to confirm to UICollectionViewDelegateFlowLayout.
As every iPhone has different width you can not set to static width.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 150)
}
As the size of the collectionView cell grows, the cell alignment between the small screen device models (iphone 5s / se / 6/7/8) and the large screen device models (iphone 8+ / XR / XS max) changes.
Like the picture below, I want to use the above picture with cell with almost no margin of collectionView.
How can I resize the collectionView cell from device to device?
optional func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
here if you are setting the size of the collectionviewcell make the width of cell
return CGSize(width: UIScreen.main.bounds.width/ 2, height:UIScreen.main.bounds.height/2)
Please set width and height property according to your need i have only divide it by 2 for simplicity .
I am working on an iOS app in swift 4. I have implemented collection view and there are two cells at the front as shown in the image:
When I run my app on bigger screen sizes like iPhone 6/7/8 Plus, then it Shows two complete cells with one-half cell. What I want to achieve is that it shows two cells in front of every screen size, Please help.
Implement the UICollectionViewDelegateFlowLayout protocol in your ViewController, then implement:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellSize = //calculate cell size based on the width of the view and padding between cells
return cellSize
}
Check out the documentation for the UICollectionViewDelegateFlowLayout and implement its methods to modify the spacing between the cells.
I'm trying to make an app that will show images in a UICollectionView, each row there must be 3 images. I've tried different a way like taking the whole view width and dividing it by 3, but it did not work for devices other than the iPhone 6+. Is there anyway I can make it such that it would be able to. One example of what I am trying to do is the instagrame timeline or suggested photos where the photos are in a 3x grid.
You have the right idea of taking the whole width and dividing by 3. You have to find the screen size within the code.
class CollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let width = collectionView.frame.width
let size = CGSize(width: width, height: 50)
return size
}
}
You might also have to play with minimumInteritemSpacingForSectionAtIndex and insetForSectionAtIndex to get the size and margins exactly the way you want it.