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))
}
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 a UICollectionView with a custom cell, however, I am trying to impose constraints on the UICollectionView, so it is always half the width of the screen and half the height of the screen. The one issue I continually encounter is that while the UICollectionView changes according to what device the app is deployed on, the UICollectionViewCells hold the exact same pixel width and height dimensions. This is the code I have tried to implement to make each cell the same height and width as the CollectionView.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let height = collectionView.frame.height
let width = collectionView.frame.width
return CGSize(width: width, height: height)
}
This code, however, doesn't work as the size of the cells are simply the values that are specified in the size inspector for the UICollectionView.
I'm unable to figure out what I am doing wrong here with my code. Thank you very much in advance for your help.
The issue is your collection view cell size is fixed always. So the cell size is not adjust according to screen size.Use view frame size.(remove values from size inspector)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
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.
I have a UICollectionView with horizontal scroll and variable sized items.
Is it possible to have constant spacing between items in 'horizontal' rows?
Thanks.
You need to provide cell's variable width. Implement this method from UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = (items[indexPath.item] as NSString).size(attributes: nil)
return CGSize(width: size.width + yourTextPadding, height: yourCellHeight)
}
You can also provide label's font attributes for exact computation or just use some constant (like 2 times width of text).
I have spent 3 days of searching and reading. In my IOS app I have a custom Cell for UICollectionView with xib File. In this custom cell I have 3 labels, images and buttons. 1 label from it is in the middle and always gets different sizes because it's multiline.
So I must calculate all items height. It's not the problem but the problem is only with this one multiline label. So I want calculate the size for each cell and get the size for sizeForItemAtIndexPath in UICollectionViewController.
After 3 days I think I cannot access this. Can I do this from the custom cell class? If yes, how? If I can access it in UICollectionViewController, how?
//MARK: - CollectionView Flow Layout
extension PhotosCollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let viewSize = super.view.bounds.size
let spacing: CGFloat = 0.5
let width = (viewSize.width) - spacing - 10
/*
Here I need my cell with label name "Beschreibungstext"
to calculate the hight from the label with help from rectangle.
return CGSize(width: width, height: customhight)
*/
return CGSize(width: width, height: CGFloat(400))
}
}