Swift CollectionView滚动超出范围

问题描述

我有一个CollectionView,问题是我必须设置.masksToBounds = false才能正确显示我的Cells(具体来说,他们的shadow)。但是,这会导致CollectionView超出我当然不希望的范围。有没有办法只在maskstToBoundsleft上设置right?还是有其他解决方法可以在这里解决我的问题?

enter image description here

您可以看到,即使我设置了theCollectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,constant: 30)

,它也会在顶部滚动

我希望我的问题很清楚。如果您需要更多信息,请告诉我。

解决方法

你有两个问题,对吧?

首先,您要在特定部分设置阴影

layer.shadowOffset = CGSize(width: 2,height: 0) 

如果你只想要左右两边的阴影,你可以将高度设置为零。

第二,你要设置位置

theCollectionView.translatesAutoresizingMaskIntoConstraints = false
theCollectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,constant: 30).isActive = true