I have a simple UICollectionViewController in my Storyboard, with custom cells with a single UILabel inside of them.
When I run the project with mock data the cells have the same width as the labels even though I made the controller conform to UICollectionViewDelegateFlowLayout and added the following code:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.collectionView.frame.width / 2.5, height: 100)
}
Does anyone know what the problem is?
In the storyboard, select the collection view and switch to the Size inspector. You will see something like this:
Change Automatic to None.
if you are using Storyboards, try giving Label inside a view and give leading as well as trailing constraints accordingly.
hope it solves your problem.
Related
I have UICollectionView inside UITableViewCell. If I make the size of the CollectionviewCell as custom and provide fixed width then it works fine.
But if I make it automatic and assign size in the delegate then it doesn't work. It takes the default width i.e 50.
Can you please help me out?
Thanks
Code for changing size:
extension BeInspiredTVC : UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width - 50, height: 224)
}
}
I want a full width Collectionviewcell but I am getting like the image below.
take collection view width constraint
next assign self.contentView.frame.width to that constraint in layoutsubsview in tableview cell.
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))
}
I am loading web view and image view on basis of condition. i am unable to manage height for collection view.
Please write some code, so we could help you better.
Also check this method it could help you:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//You can set a condition here to check if the collection has an imageView or web view and set the size you want according to it.
return CGSize(width: size, height: size)
}
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 using a collection view inside a normal view controller using the collection view delegate and data source. I'm using the sizeForItemAtIndexPath but it doesn't resize the cell.
let screenSize: CGRect = UIScreen.main.bounds
func collectionView(_collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSize(width: screenSize.width / 3, height: screenSize.width / 3)
}
The collection view is assigned the constraints using autoLayout properly.
How it looks in iPhone5
How it should look
I know the estimatedItemSize is used but I'm unable to use it since this is not a UICollectionViewController. Thanks for any suggestion?
Make sure that:
Confirming to UICollectionViewDelegateFlowLayout.
collectionView's width = screen's width.
Min Spacing for cells is zero. You can change it from the Interface Builder (Select the collectionView -> show size inspector -> set min spacing to 0), or by implementing minimumInteritemSpacingForSectionAt returning zero.
I hope that answered your question.