Autolayout约束在UICollectionView中移位

时间:2018-06-03 02:08:19

标签: ios swift scroll uicollectionview autolayout

我正在开发一个使用UICollectionView的应用程序,其中包含垂直滚动的单元格。我使用AutoLayout设置了大部分界面。这是AutoLayout设置:

AutoLayout

然而,滚动不会产生我想要的结果。偶尔,约束会搞砸,GradientViewTextView会出现迷失方向。 Here is a video displaying the scrolling effect.

这是我期望发生的事情:

Expectation

这是实际发生的事情:

Reality

背景中的紫色是Main ButtonMain ButtonShadow位置正确,但Gradient ViewText View未正确定位。 Gradient ViewText View分别具有浅灰色和白色背景颜色,如图所示,位置不正确。

我尝试更新scrollViewDidScrollcellWillDisplay方法中的约束,但都没有产生任何影响。

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    bigCollection.updateConstraints()
    for cell in bigCollection.visibleCells {
        cell.updateConstraints()
    }
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    cell.updateConstraints()
}

滚动时更改约束的可能原因是什么?提前谢谢。

1 个答案:

答案 0 :(得分:-1)

GradientView的顶部,左侧,右侧将为零(0),您的'mainButton'和GradientView的底部将类似于30(根据您的需要)到TextView。 与TextView相同。使用mainButton,left,right,bottom将为0,而对于GradientView,Top将为30。

您的GradientView和TextView约束设置为主视图,这就是它发生的原因。你可以试试我的建议,让我知道吗?