我希望我的单元格宽度在每种屏幕尺寸上都与屏幕宽度相同。我无法在 Storyboard 中设置约束,因为该选项显示为灰色,并且以编程方式设置它们会导致出现类似“无法使用 anchor 激活约束......因为它们没有共同祖先”之类的错误,即使我是仅将约束设置为 View 的前导和尾随 anchor 。
Storyboard现在的设置方式,单元格在 iphone SE 上处于所需的宽度,在较大的屏幕尺寸上具有相同的宽度。
最佳答案
你可以使用委托(delegate)方法
viewcontroller:UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize.init(
width: collectionView.bounds.height,
height: collectionView.bounds.height
)
}
关于ios - 如何在 UICollectionViewCell 上设置约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52884906/