【问题标题】:Collection view overlays on top of navigation bar集合视图覆盖在导航栏顶部
【发布时间】:2022-11-23 19:51:02
【问题描述】:

基本上我有这个简单的代码,它在 UIPageViewController 中呈现 UICollectionView

import UIKit

class ViewController: UIPageViewController {
  private let pages: [UIViewController] = [
    Page1VC()
  ]
  
  override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
    super.init(transitionStyle: .scroll, navigationOrientation: navigationOrientation, options: options)
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    setup()
  }
  
  private func setup() {
    navigationItem.title = "Home"
    setViewControllers([pages[0]], direction: .forward, animated: true)
  }
}

class Page1VC: UIViewController {
  private var collectionView: UICollectionView!
  
  private var dataSource: UICollectionViewDiffableDataSource<Section, String>!
  
  fileprivate enum Section {
    case main
  }
  
  private let cellIdentifier = "CELL"

  override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = .systemBackground
    
    let layout = UICollectionViewFlowLayout()
    layout.itemSize.height = 100.0
    collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
    
    collectionView.register(CustomCell.self, forCellWithReuseIdentifier: cellIdentifier)
    
    view.addSubview(collectionView)
    
    configureDataSource()
  }
  
  private func configureDataSource() {
    dataSource = UICollectionViewDiffableDataSource<Section, String>(collectionView: collectionView) { [weak self] (collectionView, indexPath, item) -> UICollectionViewCell? in
      guard let self = self else { return nil }
      
      
      print("CEll")
      
      let cell = collectionView.dequeueReusableCell(
        withReuseIdentifier: self.cellIdentifier,
        for: indexPath
      ) as! CustomCell
      cell.set(user: item)
      
      return cell
    }
    collectionView.dataSource = dataSource
    
    var snapshot = NSDiffableDataSourceSnapshot<Section, String>()
    snapshot.appendSections([.main])
    
    var items: [String] = []
    for i in 1...100 {
      items.append(String(i))
    }
    
    snapshot.appendItems(items)
    dataSource.apply(snapshot, animatingDifferences: false)
  }
}

class CustomCell: UICollectionViewCell {
  lazy var title: UILabel = {
    let label = UILabel()
    label.backgroundColor = .red
    label.textAlignment = .center
    return label
  }()
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    addSubview(title)

    title.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      title.heightAnchor.constraint(equalTo: heightAnchor),
      title.widthAnchor.constraint(equalTo: widthAnchor),
      title.centerXAnchor.constraint(equalTo: centerXAnchor),
      title.centerYAnchor.constraint(equalTo: centerYAnchor)
    ])
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  func set(user: String) {
    title.text = user
  }
}

当我滚动集合视图时,它会覆盖在导航栏的顶部。或者导航栏的背景是透明的?没有把握。

只有当我将 transitionStyle 设置为 .scroll 时才会发生这种情况,

  override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
    super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: options)
  }

当我使用.pageCurl时,它工作正常,

【问题讨论】:

    标签: ios swift uikit


    【解决方案1】:

    我遇到过同样的问题。我在 viewDidLoad 中添加了这个,它有帮助:

    navigationController?.view.backgroundColor = .white
    edgesForExtendedLayout = UIRectEdge.bottom
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 2012-03-24
      • 2015-12-18
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多