CollectionViewController Header 从屏幕边缘开始 Swift 4
CollectionViewController Header start from screen edge Swift 4
我有一个 collectionviewcontroller 并在代码中添加了一个 header 视图。 header 从状态栏下方开始,所以它看起来很奇怪,因为我的 header 是彩色的,而我的 collection 视图是白色的。
有什么办法可以解决这个问题,或者至少得到颜色。我尝试使用一个视图,但它随后出现在 header 上方,如果我更改 z 位置,它就会位于整个 collection 视图的后面。
我试过这样约束
self.collectionView.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: self.view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor)
当我运行时,状态栏区域变黑,header保持在同一个位置
try this
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 60, height: 60)
let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
myCollectionView.dataSource = self
myCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
myCollectionView.backgroundColor = UIColor.gray
self.view.addSubview(myCollectionView)
我认为这是由于集合视图 contentInsetAdjustmentBehavior
属性。
只需将其设置为 never
,如下所示:
collectionView.contentInsetAdjustmentBehavior = .never
你可以尝试这样设置inset:
collectionView.contentInset.top = 44
或至少你可以像这样获取和设置颜色:
您可以制作一个 UICollectionViewController 来处理 UICollectionView 并在 Interface Builder 中激活页脚部分,
那么你可以使用下面的方法:
override func collectionView(_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath)
headerView.backgroundColor = UIColor.blue
return headerView
default:
assert(false, "kind unsupported")
}
}
我有一个 collectionviewcontroller 并在代码中添加了一个 header 视图。 header 从状态栏下方开始,所以它看起来很奇怪,因为我的 header 是彩色的,而我的 collection 视图是白色的。
有什么办法可以解决这个问题,或者至少得到颜色。我尝试使用一个视图,但它随后出现在 header 上方,如果我更改 z 位置,它就会位于整个 collection 视图的后面。
我试过这样约束
self.collectionView.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: self.view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor)
当我运行时,状态栏区域变黑,header保持在同一个位置
try this
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 60, height: 60)
let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
myCollectionView.dataSource = self
myCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
myCollectionView.backgroundColor = UIColor.gray
self.view.addSubview(myCollectionView)
我认为这是由于集合视图 contentInsetAdjustmentBehavior
属性。
只需将其设置为 never
,如下所示:
collectionView.contentInsetAdjustmentBehavior = .never
你可以尝试这样设置inset:
collectionView.contentInset.top = 44
或至少你可以像这样获取和设置颜色:
您可以制作一个 UICollectionViewController 来处理 UICollectionView 并在 Interface Builder 中激活页脚部分,
那么你可以使用下面的方法:
override func collectionView(_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionView.elementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath)
headerView.backgroundColor = UIColor.blue
return headerView
default:
assert(false, "kind unsupported")
}
}