UICollectionView, simply fit cell to width?

前端 未结 3 1709
予麋鹿
予麋鹿 2020-12-14 06:20

Here\'s a simple UICollectionView in yellow

\"enter

The red arrow sets the wid

相关标签:
3条回答
  • 2020-12-14 06:58

    There is a simple way to do this. Here is the swift code. Assumes you want one item wide and in the example my cells are 64 pts high, and I want 8 pts for side margins, and finally collectionView is the name of your UICollectionView.

    Insert this into viewDidLoad :

        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize.init(width: (UIScreen.main.bounds.width - 16), height: 64.0)
    

    This will set all the cells to default to that size. You can still override individual cells if you subclass UICollectionViewLayout or UICollectionViewFlowLayout, as mentioned in other answers. But if you simply want to have more table view like behavior this should work.

    0 讨论(0)
  • 2020-12-14 07:15

    I my case cell inset making extra shapes

    0 讨论(0)
  • 2020-12-14 07:22

    I think you need to take a look at

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    

    of UICollectionViewLayout where you can set size based on orientation and current frame.


    It would indeed seem that you simply have to do this in code.

    0 讨论(0)
提交回复
热议问题