集合视图间距破坏了用户界面

Midhun MP

我正在尝试创建一个如下所示的UI。

3列布局

为此,我使用UICollectionViewFlowLayout

为了将第一个具有全宽的单元格显示为3列,其余单元格显示为,我实现了sizeForItemAtIndexPath:minimumInteritemSpacingForSectionAtIndex方法:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    var cellSize = CGSizeZero

    if indexPath.item == 0
    {
        let width  = self.cvMedia.frame.width - 20
        let height = (width * 9)/16
        cellSize   = CGSize(width: width, height: height)
    }
    else
    {
        let width  = self.cvMedia.frame.width / 3 - 20
        let height = (width * 16)/9
        cellSize   = CGSize(width: width, height: height)
    }

    return cellSize
}



func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
{
    return 20.0
}

但是我在iPhone 6SPlus和iPhone 6Plus中得到以下输出:

实际结果

I tried by changing the item spacing from 20 to 19 and it worked on simulator, but on actual device it still shows the same behaviour. If I change the spacing values I'm getting the correct output on some device versions, and not working on some versions. I can add a device version check and based on that I can return the value. But it is not an elegant solution and it will break on future versions. Can anyone help me to solve this issue ? Thanks in advance.

Ariel

You should use the collection view's width as your parameter when making this layout.

For instance,

  1. You need 3 cells to fit the view.
  2. The cells must have 20pt spacing in between them.

Solution:

Calculate the size of the cell at runtime either on viewWillAppear() or cellForRowAtIndexPath()

Scenario:

设备的宽度是320pt。Collectionview的宽度为300pt(左右间距10pt)间距:单元格之间为20pt。电池需要3个!单元格大小:

现在..开始使用任何一种方法进行计算。

像元间距=(3-1)* 20 = 2 * 20 = 40pt

其余集合视图宽度= 300pt-40pt = 260pt

像元大小= 260pt / 3 = 86.667 pt。(约86pt)。

在代码中:

cellWidth = ( collectionView?.bounds.width - minimumSpacing * (numberOfCellsInARow - 1 ) ) / numberOfCellsInARow

同样,这仅给出cellWidth。您还需要cellHeight。您可能已经具有该单元格的长宽比。使用它,还计算cellHeight。

亲切的问候,
苏曼·阿迪卡里Suman Adhikari)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

集合视图间距破坏了用户界面

来自分类Dev

Xcode 8 GM破坏了我的用户界面

来自分类Dev

为什么angularJS的用户界面视图会破坏Bootstrap Formhelper

来自分类Dev

集合视图间距很宽

来自分类Dev

多个视图模型破坏了基因敲除

来自分类Dev

Prestashop插入类别会破坏用户界面

来自分类Dev

为什么我的ObservableObject似乎被特定的视图破坏了?

来自分类Dev

Django 2.2破坏了以前的工作视图/ URL

来自分类Dev

jsf视图过期意味着会话被破坏了吗?

来自分类Dev

尝试使用chroot用户,但破坏了SSH访问

来自分类Dev

Visual Studio 2015更新1破坏了git存储库界面

来自分类Dev

PageTabViewStyle破坏了NavigationView

来自分类Dev

在为Windows用户修复了问题6之后,针对Linux用户的php-cs-fixer被破坏了

来自分类Dev

Xfce:从Squeeze升级到Wheezy破坏了xfce的用户策略

来自分类Dev

Slimscroll在AngularJs的用户界面视图中不起作用

来自分类Dev

嵌套的SplitContainers破坏了SplitterWidth

来自分类Dev

我破坏了封装吗?

来自分类Dev

.htaccess破坏了我的网站

来自分类Dev

InstallShield破坏了TFS构建

来自分类Dev

wget破坏了内容的配置

来自分类Dev

Serilog破坏了多个日志?

来自分类Dev

PHP输出破坏了Javascript

来自分类Dev

我已经破坏了Ubuntu!

来自分类Dev

添加ManytoManyField破坏了Django

来自分类Dev

导航被display:inline破坏了

来自分类Dev

Tkinter破坏了Toplevel的根源

来自分类Dev

PHP破坏了我的代码

来自分类Dev

icacls破坏了胜利10

来自分类Dev

Firebase模块破坏了Aurelia