首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iPhone 4-5上只填充一个单元格的集合视图

在iPhone 4-5上只填充一个单元格的集合视图
EN

Stack Overflow用户
提问于 2017-03-20 07:27:27
回答 2查看 181关注 0票数 0

我的视图控制器中有一个集合视图。

但它在iPhone 6-7及以上运行良好。但在iPhone 4和5中只显示了一个单元!

iPhone 5:

iPhone 6:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-20 07:42:08

它在4.7英寸及以上的iPhones屏幕上工作良好,因为它们的is宽度足够显示两个单元格(具有它们的默认宽度)。

您应该做的是确定单元格的大小,实现:

集合​视图(方法来自​视图​代理​流​布局协议。如下所示:

代码语言:javascript
复制
// 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)
    }

    // ...
}

通过实现这一点,无论设备屏幕的大小如何,单元格宽度都将是屏幕的一半。

注意:确保集合视图的最小空间为零:

此外,检查这个问答可能对您的情况很有用。

希望这能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2017-03-20 07:41:11

为每个屏幕设置CollectionViewCell大小动态。你可以试试这样的..。

代码语言:javascript
复制
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(_myCollection.frame.size.width/2-13, _myCollection.frame.size.width/2-13);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42897985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档