0

I have a collection view controller and I set up it like this:

class r : UICollectionViewCell {

    override var bounds: CGRect {
        didSet {
            contentView.frame = bounds
        }
    }
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath) as? r

        cell.backgroundColor = UIColor.red

        return cell


    }
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 5
    }

I want that the cell's width is equals to screen's width , for this reason in storyboard I edited cell's width:

enter image description here

but I have a problem :

if I execute it on iPhone 6 simulator I get this(and this is what I want to get on all devices) :

enter image description here

but If execute on iPad Pro I get this :

enter image description here

And I don't understand why. Can you help me?

P.S I don't want to use tableview for some reasons

alexander
  • 149
  • 1
  • 2
  • 14
  • On runtime the size of the cell remains the same as you set in the interface builder. It doesn't adapt to the screen size of the device. Even the auto layout constraints can't fix this issue. You need to update the item size of the collection view's layout. – Adeel Miraj Nov 06 '16 at 19:00
  • but for update the cell's size on runtime I must override the method sizeForItemAt and calculate manually the cell's content size or there is a way to do this automatically with storyboard? – alexander Nov 06 '16 at 19:08
  • No you can't do that in storyboard. You'll have to calculate that manually in the code. You need to tell what content will you be showing in the cells then I may help you calculate the height of the cell. – Adeel Miraj Nov 06 '16 at 19:12
  • I don't understand because I don't to do that automatically. with uitablewview is enough to set estimate height and then it will do automatically based in content, why with collection view I don't to do that? How do you help me? – alexander Nov 06 '16 at 19:42
  • Well that's how it is. There's no such thing for UICollectionViews. Just tell me what is the content of your cell. I may suggest you a solution. – Adeel Miraj Nov 06 '16 at 19:49
  • http://stackoverflow.com/questions/40325327/adjust-cellsize-for-fit-all-screens/40325452#40325452 – Joe Nov 07 '16 at 12:53
  • also look at my answer http://stackoverflow.com/questions/40413273/how-to-set-the-collection-view-cell-size-exactly-equal-to-the-collection-view-in/40413849#40413849 – Joe Nov 07 '16 at 12:57

4 Answers4

1

In iOS 10 there is a minor difference in function prototype: Here is Swift3.0 Delegate method for sizeAtItemAtIndexPath:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
        let screenRect: CGRect = UIScreen.main.bounds
        let screenWidth: CGFloat = screenRect.size.width
        let screencellHeight: CGFloat = screenRect.size.height
        let cellWidth: CGFloat = screenWidth / CGFloat(2)
        let cellHeight:CGFloat = screencellHeight / CGFloat(10.0)
        let size: CGSize = CGSize(width: cellWidth, height: cellHeight)
        return size
    }
Mughees
  • 607
  • 1
  • 6
  • 15
1

If u use Constraint so first of all your cell to apply equal width constraint and also storyboard in you should check collection view setting in inspect editor for horizontal and vertical.proper all setting..

ronak patel
  • 400
  • 5
  • 17
0

I assume you have set a fixed width for your cell somewhere. Maybe as constraint. Set it dynamically:

  • Conform to UICollectionViewDelegateFlowLayout

and override:

@objc(collectionView:layout:sizeForItemAtIndexPath:)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let desiredNumberOfCollumns: CGFloat = 1
    let width = collectionView.frame.width / desiredNumberOfCollumns
    let height = 100 // your desired height

    return CGSize(width: width, height: hight)
}
shallowThought
  • 19,212
  • 9
  • 65
  • 112
  • With your method I have fixed height but in the cells there will are uilabel and other views and I want that cell has a dynamic height based on content – alexander Nov 06 '16 at 18:42
0

I think you are using constraints. If yes, then all you have to do is just fix the width of your cell in constraints.

Unless you didn't change that. No matter where you have set the width ind delegates.It will remain same.

User511
  • 1,456
  • 10
  • 28