iOS11 UICollectionSectionHeader剪辑滚动指示器

Cod*_*nja 6 ios uicollectionview swift ios11

我创建了一个单一的视图项目并添加了一个collectionView.我注册了一个UICollectionReusableView的简单子类

final class TestReusableView: UICollectionReusableView {
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.red
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

将datasource和delegate设置为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)
  }
}
Run Code Online (Sandbox Code Playgroud)

结果是节标题似乎位于垂直滚动指示器上方.但是,如果我针对10.3.1模拟器运行应用程序,行为就像我期望的那样.

红色部分标题位于滚动指示器的顶部

rps*_*stw 6

这对我有用:

- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    if (SystemVersionGraterOrEqual(11)) {
        if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
            view.layer.zPosition = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

}


Ome*_*lik 1

我无法尝试,但你可以尝试将其添加到你的viewDidLoad()

let flow = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.sectionHeadersPinToVisibleBounds = true
Run Code Online (Sandbox Code Playgroud)