【问题标题】:collectionView divided into three lines swift 4collectionView 分为三行 swift 4
【发布时间】:2019-02-11 09:44:27
【问题描述】:

我需要创建一个 collectionView 来显示一系列图像,分别为每张照片。我正在尝试重现您在图片中看到的内容,查看照片,然后向左或向右滚动以查看下一张。我无法创建collectionView的布局

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.datasource.count
}

//UICollectionViewDelegateFlowLayout - constraint della collecion view da innestare
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = (UIScreen.main.bounds.size.width - 3) / 3
    let height = width
    return CGSize(width: width, height: height)
}

【问题讨论】:

  • 如果您希望单元格占据整个屏幕宽度,请不要除以 3。let width = (UIScreen.main.bounds.size.width)。另外,最好不要使用屏幕尺寸。如果是ViewController,请使用let width = self.view.frame.width,或者如果是UIView,它正在呈现collectionView,请使用let width = self.frame.width。还要确保使布局水平。 layout.scrollDirection = .horizontal
  • 也许给单元格提供视图控制器的整个大小。然后在单元格中为图像视图添加一个约束,即居中图像视图并给出其单元格的总高度 * 0.3。也固定它的左右边缘。或者,如果您不希望十字架滚动,您可以将上述约束提供给您的 collectionview。

标签: ios swift iphone uicollectionview


【解决方案1】:

迅速 4:

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

     //calculation of cell size
    return CGSize(width: self.collectionview.frame.size.width,height: 180)
      }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    // indentation 
    return UIEdgeInsetsMake(0, 23, 20, 20)
    }

如果你想拖动到水平或垂直,在collectionview中有一个“滚动方向”属性。您可以将其更改为水平或垂直。

【讨论】:

  • 为什么要除以 2?这里需要全宽
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
相关资源
最近更新 更多