简体   繁体   English

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

[英]UICollectionView — scaling cells to fit the screen

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 . 我有一个包含概览视图的应用程序,所以我不想滚动UITableViewUICollectionView的项目。 On the iPhone (Compact/Regular), I want each item to scale to fit the screen of the phone; 在iPhone(紧凑型/常规)上,我希望每个项目都可以缩放以适合手机屏幕。 on the iPad (well, Regular/Regular), I'd like to display things differently. 在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是因为我不认为UITableViewUITableView的方法。

Here's a mockup of what I mean: 这是我的意思的模型: iPhone和iPad样机

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. 我在UIStackView有菱形项及其旁边的标签,并使其与UIStackView一起使用,但现在要使其制作成使所有项都适合屏幕。 Help? 救命?

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)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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