Collection View Cell Size/Fitting issue - ios

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.

Related

How to create multiline label in collectionview in swift iOS?

I have created collectionview and collectionviewcell. In that cell i have labels. i am getting long string and trying to show that in multiline. but not sure how to do this in colletctionview.
Thanks for helping
First apply constraints to you image view like this
Second Apply constraints to label
I got some solution but not sure how to put dynamic height. By following code my width is as per screen size so label is getting in multiline. but trying to get dynamic height as per content
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let kWhateverHeightYouWant = 400
return CGSize(width: collectionView.bounds.size.width, height: CGFloat(kWhateverHeightYouWant))
}

How to make rounded collection cells fit on all screens?

So I'm normally use to making square sized collection view cells fit perfectly on all screens. However once I start adding spacing and section insets I run into this problem. I am also using the storyboard.
This is how I would like all of the cells to look on each screen.
iphoneX
I have the minimum space for cells and lines at 10.
And for the right and left section insets they're set to 10 too.
This is the result I get on other devices ( iPhone 7/XsMax)
You have to set cell size programmatically by implementing func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize method which is in UICollectionViewDelegateFlowLayout protocol.
In this method you can set collection view cell's width
(UIScreen.main.bounds.size.width/2)-10

Dynamic Buttons in UIScrollView with Equal width and height

I want to display list of buttons in scrollview with dynamic height based on screen size. I can able to do this in UIView, but when I applied same auto layout approach in UIScrollView, it's not increasing the size of buttons. So please guide me How to increase the button size in UIScrollView based on screen size.
if button is dynamic and may be increase and decrease in future than you Should use CollectionView not ScrollView.
Drag CollectionView into ViewController.
Drag Cell in CollectionView.
Add button(or label based on your requirement) In Cell.set button(leading,trailing,top,bottom) == Cell(leading,trailing,top,bottom)
Write dataSource and delegate methods and UICollectionViewDelegateFlowLayout methods.
and change Following Methods to increase button height According To Screen
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.view.bounds.width/2, height: self.view.bounds.height*0.15)
}

UICollectionView 3x in one row

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.

UICollectionView spacing between rows becomes huge on certain devices

I have a collection view with the minimum spacing set to 10 for cells and lines. The horizontal space between the cells adjusts correctly, but I don't know how to alter the vertical spacing between the rows of cells. On the 6, which it was originally developed on, the space is negligible and works fine. But on every other device, the space quadruples in size for no apparent reason. Is there any way to set the spacing between the rows of cells to be a constant value across all devices?
Right now there isn't a way to set the size of collection view cells using AutoLayout. My guess is that the size of your cells just doesn't look good on other screen sizes, so you most likely will need to implement the UICollectionViewDelegateFlowLayout protocol (which extends the UICollectionViewDelegate protocol) and the func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize method.
Although if that's not the case maybe posting some screenshots would help.
Example method:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if(self.view.bounds.width > 400){
return CGSize(width:self.view.bounds.width/3-10, height:self.view.bounds.width/3-10)
}
return CGSize(width:self.view.bounds.width/2-10, height:self.view.bounds.width/2-10)
}

Resources