UICollectionView - 缩放单元格以适合屏幕

时间:2021-12-17 21:13:38

I have an app that includes an at-a-glance view, so I don't want to scroll the items in a UITableView or UICollectionView. On the iPhone (Compact/Regular), I want each item to scale to fit the screen of the phone; on the iPad (well, Regular/Regular), I'd like to display things differently.

我有一个包含一目了然的视图的应用程序,所以我不想在UITableView或UICollectionView中滚动项目。在iPhone(紧凑/常规)上,我希望每个项目都能缩放以适应手机的屏幕;在iPad上(好吧,常规/常规),我想以不同的方式展示。

Note: This question is about the phone, only; I mention the iPad because I don't think UITableView is the way to go.

注意:这个问题仅与手机有关;我提到了iPad,因为我认为UITableView不是最佳选择。

Here's a mockup of what I mean: UICollectionView  - 缩放单元格以适合屏幕

这是我的意思的模型:

I've got the diamond item and the label beside it in a UIStackView, and got this working with a UICollectionView, but now want to make it so all the items fit on screen. Help?

我在UIStackView中得到了钻石项目和它旁边的标签,并且使用了UICollectionView,但现在想要使所有项目都适合屏幕。帮帮我?

1 个解决方案

#1


1  

you can use this collectionView flow layout delegate method to acheive this

您可以使用此collectionView流布局委托方法来实现此目的

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
  if UIDevice.current.userInterfaceIdiom == .pad{
     // here return cell size for iPad.
     return CGSize(width:collectionView.bounds.width / 2 , height:100)
  }else{
      // here return cell size for iPhone
      return CGSize(width:collectionView.bounds.width, height:100)
  }
}

#1


1  

you can use this collectionView flow layout delegate method to acheive this

您可以使用此collectionView流布局委托方法来实现此目的

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
  if UIDevice.current.userInterfaceIdiom == .pad{
     // here return cell size for iPad.
     return CGSize(width:collectionView.bounds.width / 2 , height:100)
  }else{
      // here return cell size for iPhone
      return CGSize(width:collectionView.bounds.width, height:100)
  }
}