Tim Sullivan
Tim Sullivan

Reputation: 16898

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: iPhones and iPad mockups

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?

Upvotes: 0

Views: 1103

Answers (1)

Irshad Ahmad
Irshad Ahmad

Reputation: 1383

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

Upvotes: 1

Related Questions