我使用 insetForSectionAtIndex 方法为我的 Collection View 中的一个部分设置 contentInset,我不想将该插图应用于该部分的标题。我需要标题宽度与屏幕一样宽。
内容插入
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
if section == 1 {
return UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
}
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
标题
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: opsMainDescriptionSegmentedControlCellId, forIndexPath: indexPath) as! MyHeader
return header
}
最佳答案
我只需要在我的一个项目中做同样的事情。
我应用的解决方案:
在 Storyboard 中,我的 Collection View 占据了具有自动布局约束的全屏。
在viewDidLoad中:
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
我将 collectionView 委托(delegate)设置为 self 并且也符合
UICollectionViewDelegateFlowLayout 协议(protocol)。
然后您可以访问您在我返回另一个 UIEdgeInsets 时使用的方法
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 15, left: 20, bottom: 15, right: 20)
}
它对我有用。
看看最后的样子。
希望这能够帮助到其他人。
关于ios - 如何仅对内容单元格而不是 UICollectionView 中的标题应用不同的 contentInset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474482/