Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS11 UICollectionSectionHeader clipping scroll indicator

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.

Red section header is on top of the scroll indicator

like image 834
CoderNinja Avatar asked Oct 14 '17 18:10

CoderNinja


3 Answers

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;
    }
}

}

like image 99
rpstw Avatar answered Oct 21 '22 22:10

rpstw


I couldn't try it out but can you try adding this to your viewDidLoad():

let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.sectionHeadersPinToVisibleBounds = true
like image 35
Omer Celik Avatar answered Oct 21 '22 23:10

Omer Celik


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.

like image 1
Naveen Avatar answered Oct 21 '22 21:10

Naveen