当CollectionView只有一个项目时如何使垂直uiCollectionViewCell左侧

问题描述 投票:0回答:2

当CollectionView只有一个项目时,如何使垂直uiCollectionViewCell左侧My collectionView looks like

I want to make my CollectionView like this. How can I do make like this

ios swift xcode uicollectionviewcell
2个回答
0
投票

我认为这是iOS中的错误,由于UICollectionViewFlowLayoutAutomaticSize,该错误会自动将CollectionView中的单个单元格居中。您可以创建自己的UICollectionViewFlowLayout

我遵循这里给出的方法:

Stop Single UICollectionView cell Flowing to the centre of the screen


0
投票
 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    collectionviw.delegate = self
    collectionviw.dataSource = self
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 5, bottom: 10, right: 2)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    collectionviw!.collectionViewLayout = layout
    collectionviw.reloadData()
}

extension ViewController: UICollectionViewDelegate, 
 UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 
section: Int) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt i 
 ndexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionviw.dequeueReusableCell(withReuseIdentifier: "cell", 
for: indexPath)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout 
  collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: 
 IndexPath) -> CGSize {
          let collectionWidth = collectionView.bounds.width / 2 - 50
          return CGSize(width: collectionWidth, height: collectionWidth)
      }
  }
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.