How to make rounded collection cells fit on all screens? - ios

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

Related

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 with paging that works for both portrait and landscape

I have a project that has UICollectionView which needs to have paging. I have set horizontal scroll and paging enabled. MinimumInteritemSpacing is 0. I need the cells to be the size of the collection view so I have:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return collectionView.frame.size
}
This sizes the cells properly for both portrait and landscape, but the offset on orientation change is wrong and two cells will be displayed at the same time. What is a good approach to be able to see the same cell on orientation change and for it to be taking up the whole collection view?
I have tried implementing collectionView(_:targetContentOffsetForProposedContentOffset:) This will fix the issue if you stay on one screen, but going to another screen, rotating and going back to the collection view screen will have a bug - there will be again two partially visible cells and the offset will not be the wanted one. This is why I guess there should be another solution to this issue.

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.

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)
}

How to make UICollectionViewCell's width auto resize and its height stay the same

I want my custom UICollectionViewCell's width dynamically changes depending on the UILabel content inside, but each cell should stay the same height. Using estimatedItemSize will also change the height value (which isn't what I want, I need it to stay the same, say 30.)
And after reloading data for several times it starts being like an chaos, making some cells not vertical align.
Did you try implementing
collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
Finally I solved the problem by using sizeWithAttributes in sizeForItemAtIndexPath to dynamically determine the width of each cell should be.

Resources