简体   繁体   中英

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 . 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.

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

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. Help?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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