简体   繁体   中英

Change UICollectionViewCell size according to the size of the screen

I have a UICollectionViewController in storyboard with Collection Header View and one dynamic UICollectionViewCell . I add new cells progrmatically. I want to build interface as two portrait cell in one row including image in it and further cell as same. I have added constraints to Collection Header View and UICollectionViewCell .

It works fine for the device with the screen size that I used for Storyboard but does not change its size according to the other screens.

The size of the UICollectionViewCell stays same. For example, In iPhone 4s only one cell fits and further cell below it one by one. In the iPhone 6s screen two portrait cells in one row but with more gap between them. In iPhone 6 it fits perfectly because I have used that size for storyboard .

I have tried sizeForItemAtIndexPath: method but again I am giving static size here which stays same and does not change accordingly.

- (CGSize)collectionView:(UICollectionView *)collectionView layout (UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath (NSIndexPath *)indexPath {
    return CGSizeMake(160, 256); }

SELF ANSWER

The answer I am going to provide is not feasible, but it works fine for me.

I have used the code shown below to resize UICollectionViewCell width according to the UIView size.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.view.frame.size.width / 2.2, 256); }

Here static height is fine for me because it allows to scroll.

You set the custom method for uicollectionviewlayout for iphone5 and iphone6 etc.Its like above code..

@property (strong,nonatomic)uicollectionviewlayout *objLayout;
[self setlayout];
-(void)setlayout
{
if(iphone5s)
{
  objLayout.itemSize = CGSizeMake(cellSize, cellSize);
}
else
{
  objLayout.itemSize = CGSizeMake(cellSize, cellSize);
}
[self.menuCollectionView setCollectionViewLayout:objLayout];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM