Swift-如何针对不同的IOS设备大小调整UICollectionViewCell的比率 [英] Swift - How to adjust UICollectionViewCell's ratio for different IOS device sizes

查看:59
本文介绍了Swift-如何针对不同的IOS设备大小调整UICollectionViewCell的比率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CollectionView 作为我底部的菜单栏 ViewController 我创建了一个自定义类,并制作了5个均匀间隔的单元格,用以下内容填充了iPhone 8上的整个 CollectionView ,这是所需的结果-

I'm using a CollectionView as a menu bar at the bottom of my ViewController I've created a custom class and have made 5 evenly spaced cells which fills the entire CollectionView on an iPhone 8 with the following, which is the desired result -

    layout.itemSize = CGSize(width: self.frame.width / 5, height: self.frame.height)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0

    self.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    self.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    self.collectionViewLayout = layout;

但是,当我在具有更大屏幕的任何iPhone上运行该项目时,都会得到以下结果:

However when I run the project on any iPhone with a larger screen I get the following result:


我的问题是,即使需要 CollectionView 来增加其高度,我如何缩放单元格也可以使其保持高度,我想将这些图像放置在单元格)以从iPhone 8屏幕保持其宽高比.

My question is, how could I scale the cells up to fit without any margins, even if it requires the CollectionView to increase it's height, I would like the images (which will be placed in the cells) to maintain their aspect ratio from the iPhone 8 screen.

我可以使用代码增加每个 CollectionViewCell 的大小吗?如果可以的话,有没有办法解决目前发布的每部iPhone的宽度/高度问题?

Could I just increase the size of each CollectionViewCell with code? If so is there a way to do so without working out the width/height of each iPhone released to date?

还是可以请其他人推荐其他方法来完成此操作?如果我以错误的方式来处理这个问题.

Or can someone please recommend a different way to accomplish this? if I'm approaching this the wrong way.

提前谢谢!

推荐答案

在您放置线条的地方,视图的宽度似乎不正确

It seems that the view's width isn't correct in the place you put the line

layout.itemSize = CGSize(width: self.frame.width / 5, height: self.frame.height)

因此将其替换为

layout.itemSize = CGSize(width: UIScreen.main.bounds.width / 5, height: self.frame.height)


您也可以实现


you can also implement

func collectionView(_ collectionView: UICollectionView, 
                  layout collectionViewLayout: UICollectionViewLayout, 
           sizeForItemAt indexPath: IndexPath) -> CGSize

符合 UICollectionViewDelegateFlowLayout

这篇关于Swift-如何针对不同的IOS设备大小调整UICollectionViewCell的比率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆