Collection view every next cell should visible 10% in the current screen - ios

I have many collection view cells. I want to show two collection view cells on the screen with respect to first cell (A) should visible 90% of the screen and second cell (B) should visible 10% in the present screen, if i scroll to next cell. first cell (A) visible 10% of the screen and second cell (B) visible 90% of the screen.
I was Achieved by below code.
Perfectly working if i have 2 cells
Not exactly working if i have more than 2 cells.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let value = 0.9;
let width = UIScreen.main.bounds.size.width*CGFloat(value)
return CGSize(width: width, height: collectionView.frame.height)
}
RefImage:
Expected output

Related

UICollectionView Cell not filling entire screen (to bottom)?

UICollectionView cell isn't filling the entire screen. When scrolling, it then messes up (leaving spacing on bottom because the image is too short.
Things I've tried:
-Collection View Inset is "None"
-Min spacing is 0 for cells, 0 for lines
-Image inside cell is constraint to 0 on all four sides
-UI collection View is constraint to 0 on all four sides to view controller
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// Even you can set the cell to uicollectionview size
let cvRect = collectionView.frame
return CGSize(width: cvRect.width, height: cvRect.height)
}
Storyboard is made with iPhone 12 Pro max. Simulator: iPhone 12 Pro. Each scroll has a bigger image from the previous.

UICollectionView Horizontal Scroll swiping left and right position of cell changes?

Horizontal Scroll in UICollectionView when i scroll left or right the cell not position right ? For each index item i can see the next item coming at the edge and scrolling further more of next cell is coming out to the current index. I want to show only the current index item in scroll. I dont know if its a layout issue ? Any help would be really helpfull. I set my collection view minimum spacing to 0.
You can add this code.also make sure to add UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: yourCollectionView.bounds.width, height: yourCollectionView.bounds.height)
}

UIcollectionview with 2 different kind of cells

I need to create a UIcollectionview with grids of cells with 3 cells per row. After a certain amount of cells, lets say 60, I need to display a big cell with image view inside of it, and the cells needs to be in full width of the UIcollectionview.
I have done created the 3 cells part, but I failed to figure out how to insert the big image cell and how to layout it properly.
Thanks.
How about you do a few sections? And the header is the other cell. You can put an image there as well.
You could specify the size of the cell in the UICollectionViewFlowLayout delegate method
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//logic to find if the cell is 60 th item and then and dont forget to return the size of the other cells.
if 60th cell {
return size
} else {
return size
}
}

Collection View Cell Size/Fitting issue

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.

CollectionView. Maximum distance between Item. Swift 3

I know there is only minimum spacing between items and you can achieve that via size inspector or
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
Indeed on the simulator on SE my items are shown like in the image below.
But when I run on a larger simulator my distance between cell items is increasing and aren t showing as expected.
Is there any method to set like a maximum size between them ?
Logically, there is another factor that you should keep in mind for setting the space between the items in your collection view, which is the size of the cell. If you are getting the cell as is (with its default size) -and my assumption is that's your case-, it would be statically dequeued in the collection view, i.e the size of the cell would be as is.
So, what you should do is to tell what is the desired size (width) of the cells to be displayed in the collection view, by conforming to
UICollectionViewDelegateFlowLayout and implementing collectionView(_:layout:sizeForItemAt:):
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// let's assume that each row in the collection view should contains 3 cells:
let desiredWidth = collectionView.frame.width / 3
// height is also equals to the width to be a square...
return CGSize(width: desiredWidth, height: desiredWidth)
}
At this point if you are setting the minimum space between items to zero, each row should contains three cells with no space between them.
If you are aiming to set a little space between the cells, you should reflect it when declaring the desired width for the cell, for example:
// since each row contains 3 cells, subtracting 2 from each cell width
// means it would be 6 points (3 * 2) divided at the whole row (2 spaces, 3 points for each)
let desiredWidth = collectionView.frame.width / 3 - 2

Resources