How to align collectionview cells right next to each other? - swift

I am creating a profile page screen for my application. The screen displays all the user's recent posts. I used storyboard to create two UICollectionViewCells in a UICollectionView, one that displays your profile info and the other that displays your posts. See this for how I designed it in storyboard: https://i.stack.imgur.com/6TzwK.png
When I run the application, I get the following result: https://i.stack.imgur.com/XmvY3.jpg
However, I desire the cells placed in a way that it looks like the following: https://i.stack.imgur.com/nWQ14.jpg
How do I force the cells the align so it looks like the image above? Thanks!

Found an answer to the question. Implement UICollectionViewDelegateFlowLayout delegate to your class. Then, include the following method into the class:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = UIScreen.main.bounds.width // get the width of the screen
let scale = (width / 3) // get the width (and height) of each cell
if indexPath.row != 0 { // check to see if the cell is the profile header
return CGSize(width: scale, height: scale) // if not, then return the cell size
}
return CGSize(width: width, height: ((238/414) * width)) // if it is the profile header, return the size for it.
}
Make sure "Min Spacing" for the UICollectionView's storyboard setting to 0,0. The cells will then align into a grid view.

Related

Auto-Layout for CollectionViewCell: Trailing/Leading constraints change cell size

I just can´t get familar with auto layout in Xcode. When I set trailing and leading constrains for Header Stack View, Bottom Stack View or the image between them, then the size (height/width) of my cell changes. Why does it happen and how can I avoid it? I think something isn´t right with the kind I build layouts. I am very grateful for any help.
I set the cell size with this code:
extension MainVC: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = view.frame.size.height
let width = view.frame.size.width
return CGSize(width: width * 0.42, height: height * 0.3)
}
}
My cell layout:
This is how the app looks when I use constrains for the Bottom Stack View.
This is without constrains for the Bottom Stack View (correct cell size).
Cells can now self-size with auto layout and ignore what you return in collectionView(_:layout:sizeForItemAt:). If you want to use collectionView(_:layout:sizeForItemAt:), change Estimate size to None in the storyboard.
https://stackoverflow.com/a/58369142/14351818

Setting CollectionView cell height to dynamic based on content

I have an application where I am using collectionview to display content which includes imageview, about 4 uilabels and a stackview which are all constrained to each other. This cell is scrollable vertically and not horizontally as I have to make provison for larger screen size like the iPad. The problem I have now is my CollectionViewFlowLayout height is static, How can I make it dynamic so that it resizes base on the height of the retuned content. the text of the label could vary so how do I handle this
below is how I have made my cell
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width, height: 400)
}
further code would be added on request and any help is appritiated

Corner Radius for UIImageView not working for UICollectionViewCell with dynamic size

I have cell which consist of a UIImageView which has a height of 70% of the cell size and the rest of the 30% is used up by the label.
The cell's have a dynamic size which is calculated according to the device width.
I have made sure the cells are are square and the UIImageView contained within the cell's also has 1:1 ratio for their height and width
I am setting the corner radius of the UIImage in the cellForItem delegate method of the Collection View
cell.userImage.layer.cornerRadius = cell.userImage.bounds.width / 2
cell.userImage.clipsToBounds = true
So can anyone point me in the right direction as why this is happening ?
When should I set the corner radius of the Image in the cell lifecycle ?
This doesn't happen if I give the image view static size, but I want the size to be dynamic according to the screen size
try cell.userImage.layer.maskToBounds = true
Better to wrap imageView inside a UIView . Then set UIView propertiesbound.size.width/2
You are taking the width of "bounds". Try taking the width/height of cell.userImage.
cell.userImage.layer.cornerRadius = cell.userImage.bounds.width / 2
Also, is the ratio of the image 1:1?
hey use this delegate UICollectionViewDelegateFlowLayout to set the size for each item in cell. something like below
...
collectionView.delegate = self
...
// then make your view controller confirm to the UICollectionViewDelegateFlowLayout
// then overide this method like below
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
CGSize(width: MyLayoutConstants.collectionViewItemWidth,
height: collectionView.frame.height)
}
this is just an example from my case. the main idea is that use the delagate above.

How to make 2 rows in horizontal collection view without using sections

The horizontal scrolling in UICollectionView only returns one row and I need to make a horizontal scrolling collection view with 2 rows just like the image below
[1]: https://i.stack.imgur.com/Vs1kt.png :[1]
You need to set fix height of the CollectionView and then use sizeForItemAt() under UICollectionViewDelegateFlowLayout which returns CGSize. You have to manage the cell height something like this.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 50.0, height: collectionViewHeight / 2) //<-- Manage height and width accordingly.
}

Need to move the cell inside the uicollectionview cell && Textfield image adding

need to move the collection view cell accordingly. I have attached my screenshot for your reference:
the above is my current image but I need that to be like this:
Then I need to add image to the textfield but I have added some code but I could not add it
searchtextfield.leftViewMode = UITextFieldViewMode.always
searchtextfield.leftView = UIImageView(image: UIImage(named: "search"))
For the collection view cell width and height you have to use below method:
extension "Your Controller" : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/2 - 15, height: collectionView.frame.size.width/2 - 15)
}
}
To make the the cell square I have added the with and height same. You can change it according to your requirement. The collection view width changes for different devices so you have to change the cell width and height too.
Don't forget to do the change in this too according to your requirement:
Hope this helps.