collectionview 仅填充 iPhone 上的一个单元格 4-5
collectionview just filling one cell on iPhone 4-5
我的视图控制器中有一个集合视图。
但它在 iPhone 6-7 及更高版本中运行良好。
但那只显示 iPhone 4 和 5 中的一个单元格!
iPhone 5:
iPhone 6:
为每个屏幕动态设置 CollectionViewCell 大小。你可以尝试这样的事情...
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(_myCollection.frame.size.width/2-13, _myCollection.frame.size.width/2-13);
}
它在 4.7 英寸及以上的 iPhone 屏幕尺寸上工作正常,因为它们的宽度尺寸足以显示两个单元格(使用它们的默认宽度)。
你应该做的是确定单元格的大小,就是实现:
collectionView(_:layout:sizeForItemAt:) method from UICollectionViewDelegateFlowLayout 协议。如下:
// don't forget to conform to UICollectionViewDelegateFlowLayout protocol:
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout {
//...
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenWidth = UIScreen.main.bounds.width
// if you want to let the cell to be a square,
// you should let the height equals to the width
// or you can -of course- set the deired height
return CGSize(width: screenWidth / 2, height: screenWidth / 2)
}
// ...
}
通过实现这一点,单元格宽度将是屏幕的一半,无论设备屏幕的尺寸如何。
注意:确保集合视图的最小空间为零:
还有,检查可能对你的情况有用。
希望对您有所帮助。
我的视图控制器中有一个集合视图。
但它在 iPhone 6-7 及更高版本中运行良好。 但那只显示 iPhone 4 和 5 中的一个单元格!
iPhone 5:
iPhone 6:
为每个屏幕动态设置 CollectionViewCell 大小。你可以尝试这样的事情...
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(_myCollection.frame.size.width/2-13, _myCollection.frame.size.width/2-13);
}
它在 4.7 英寸及以上的 iPhone 屏幕尺寸上工作正常,因为它们的宽度尺寸足以显示两个单元格(使用它们的默认宽度)。
你应该做的是确定单元格的大小,就是实现:
collectionView(_:layout:sizeForItemAt:) method from UICollectionViewDelegateFlowLayout 协议。如下:
// don't forget to conform to UICollectionViewDelegateFlowLayout protocol:
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout {
//...
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenWidth = UIScreen.main.bounds.width
// if you want to let the cell to be a square,
// you should let the height equals to the width
// or you can -of course- set the deired height
return CGSize(width: screenWidth / 2, height: screenWidth / 2)
}
// ...
}
通过实现这一点,单元格宽度将是屏幕的一半,无论设备屏幕的尺寸如何。
注意:确保集合视图的最小空间为零:
还有,检查
希望对您有所帮助。