Ios collectionview仅在iPhone 4-5上填充一个单元格
我的视图控制器中有一个集合视图 但它在iphone6-7及以上版本中运行良好。 但这显示iPhone4和iPhone5中只有一个手机 iPhone 5: iPhone 6:Ios collectionview仅在iPhone 4-5上填充一个单元格,ios,swift,xcode,uicollectionview,uicollectionviewcell,Ios,Swift,Xcode,Uicollectionview,Uicollectionviewcell,我的视图控制器中有一个集合视图 但它在iphone6-7及以上版本中运行良好。 但这显示iPhone4和iPhone5中只有一个手机 iPhone 5: iPhone 6: 为每个屏幕设置CollectionViewCell大小动态。你可以试试这样的 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForIt
为每个屏幕设置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);
}
为每个屏幕设置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屏幕尺寸,因为其宽度大小足以显示两个单元格(默认宽度) 您应该做的是确定单元的大小,实现: 方法从协议中删除。详情如下:
// 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)
}
// ...
}
通过实现这一点,无论设备屏幕大小如何,单元格宽度都将是屏幕的一半
备注:确保集合视图的最小空间为零:
此外,检查可能对您的案例有用
希望这有帮助。它在4.7英寸及以上的iPhone屏幕尺寸上运行良好,因为其宽度大小足以显示两个单元格(默认宽度) 您应该做的是确定单元的大小,实现: 方法从协议中删除。详情如下:
// 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 6-7..是如何显示..更新了我的问题。