I created a single view project and added a collectionView. I registered a simple subclass of UICollectionReusableView
final class TestReusableView: UICollectionReusableView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.red
}
...
}
Set datasource and delegate to self
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeader, for: indexPath)
return headerView
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = UIColor.blue
return cell
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 88)
}
}
The result is the section header seems to be positioned above the vertical scroll indicator. However, if I run the app against a 10.3.1 simulator, the behavior works as I'd expect.
this works for me:
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
if (SystemVersionGraterOrEqual(11)) {
if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
view.layer.zPosition = 0;
}
}
}
I couldn't try it out but can you try adding this to your viewDidLoad()
:
let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.sectionHeadersPinToVisibleBounds = true
Well even I encountered the same issue in my app and it was slated for release in 2 weeks. I couldn't have time to do research on why this is failing only in iOS 11. Hence the quick work around which I did is by replacing the usage of headerView with footerView since footerView doesn't have this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With