Martin Perry
Martin Perry

Reputation: 9527

UICollectionViewCell content not stretched

I have UICollectionView stretched across entire width of the screen. My UICollectionViewCells are loaded from MyViewCell.xib. I set its size via:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
   return CGSizeMake(CGRectGetWidth(collectionView.frame) * 0.5, CGRectGetHeight(collectionView.frame));
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 16;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
    return cell;  
}

And during main view controller loading I have:

[self.collView registerNib:[UINib nibWithNibName:@"MyViewCell" bundle:nil] forCellWithReuseIdentifier:@"myCell"];
self.collView.delegate = self;
self.collView.dataSource = self;

This makes "cell view" of correct size, but its content loaded from XIB is still "small" and not strechted. This means, I can see two items on the screen, but they are not strechtech, only aligned to left and have fixed width around 50.

If I use the same code on iPhone, it is working correctly, problem is only with iPad. Both devices have same iOS version (10.3.3).

Upvotes: 0

Views: 661

Answers (1)

matt
matt

Reputation: 535566

Your code, as given, works correctly. I translated it into Swift as follows (this is the entirety of my view controller's code:

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    @IBOutlet weak var cv: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.cv.register(UINib.init(nibName: "MyViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
        self.cv.delegate = self
        self.cv.dataSource = self
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 16
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = cv.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.layer.borderWidth = 2
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width:self.cv.bounds.width/2, height:self.cv.bounds.height)
    }
}

This is what I see on the iPad simulator:

enter image description here

And that, given the default interitem spacing of 10, is just what I would expect to see.

The MyViewCell nib contains just a small yellow cell. I added the border just for clarity.

Upvotes: 1

Related Questions