我正在开发一个使用UICollectionView的应用程序,其中包含垂直滚动的单元格。我使用AutoLayout设置了大部分界面。这是AutoLayout设置:
然而,滚动不会产生我想要的结果。偶尔,约束会搞砸,GradientView
和TextView
会出现迷失方向。 Here is a video displaying the scrolling effect.
这是我期望发生的事情:
这是实际发生的事情:
背景中的紫色是Main Button
。 Main Button
和Shadow
位置正确,但Gradient View
和Text View
未正确定位。 Gradient View
和Text View
分别具有浅灰色和白色背景颜色,如图所示,位置不正确。
我尝试更新scrollViewDidScroll
和cellWillDisplay
方法中的约束,但都没有产生任何影响。
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()
}
滚动时更改约束的可能原因是什么?提前谢谢。
答案 0 :(得分:-1)
GradientView的顶部,左侧,右侧将为零(0),您的'mainButton'和GradientView的底部将类似于30(根据您的需要)到TextView。 与TextView相同。使用mainButton,left,right,bottom将为0,而对于GradientView,Top将为30。
您的GradientView和TextView约束设置为主视图,这就是它发生的原因。你可以试试我的建议,让我知道吗?