在 UIView 中调整 CollectionViewCell 的大小
Resizing CollectionViewCell in UIView
我在 UIView 中有 CollectionView。如何将 CollectionViewCell 调整为屏幕宽度的 50%?我想在 CollectionView 中创建 2 列。
class Class_MyUIViewClass: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
我在 CollectionViewCell 中也有图像和标签。
您可以使用 UICollectionView 的 sizeForItemAt
方法来管理它的单元格大小
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width
return CGSize(width: screenWidth/2, height: yourHeight)
}
也确认到UICollectionViewDelegateFlowLayout
假设您知道所需的单元格高度,请符合 UICollectionViewDelegateFlowLayout
:
class Class_MyUIViewClass: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
并实施 sizeForItemAtIndexPath
:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let desirednumberOfCollumns: CGFloat = 2
let width = collectionView.frame.width / desirednumberOfCollumns
return CGSize(width, yourHeight)
}
我在 UIView 中有 CollectionView。如何将 CollectionViewCell 调整为屏幕宽度的 50%?我想在 CollectionView 中创建 2 列。
class Class_MyUIViewClass: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
我在 CollectionViewCell 中也有图像和标签。
您可以使用 UICollectionView 的 sizeForItemAt
方法来管理它的单元格大小
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width
return CGSize(width: screenWidth/2, height: yourHeight)
}
也确认到UICollectionViewDelegateFlowLayout
假设您知道所需的单元格高度,请符合 UICollectionViewDelegateFlowLayout
:
class Class_MyUIViewClass: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
并实施 sizeForItemAtIndexPath
:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let desirednumberOfCollumns: CGFloat = 2
let width = collectionView.frame.width / desirednumberOfCollumns
return CGSize(width, yourHeight)
}