UICollectionview 如何通过滚动避免顶部栏

UICollectionview how to avoide top bar by scrolling

当我启动我的应用程序时一切正常。

As it should stay.

但是当我开始向下滚动时,顶部出现了一个奇怪的栏。 Strange Bar by Scrolling down

我在寻找一些框架问题,但找不到。我不知道是什么导致了这种行为。我以编程方式编写了我的 UICollectionview。

这是我的代码:

    import UIKit

// MARK: Erzeugung & Füllen des Daten STRUCT
struct Profile {
    let name: String
    let location: String
    let imageName: String
    let profession: String
}

var profiles: [Profile] = []

private func populateProfiles() {
    profiles = [
        Profile(name: "Thor", location: "Boston", imageName: "astronomy", profession: "astronomy"),
        ...
        Profile(name: "Elon Musk", location: "San Francisco", imageName: "graduate", profession: "graduate")
    ]
}

class ViewController: UIViewController {

    private let collectionView: UICollectionView = {
        let viewLayout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: viewLayout)
        // collectionView.backgroundColor = .white
        return collectionView
    }()
    
// MARK: Hintergrundbild erzeugen
    let imageView : UIImageView = {
        let iv = UIImageView()
        iv.image = UIImage(named:"Backround-wood")
        iv.contentMode = .scaleAspectFill
        return iv
    }()

    private enum LayoutConstant {
        static let spacing: CGFloat = 50.0 //Größe der Zwischenräume
        static let itemHeight: CGFloat = 500.0 //Zellhöhe
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        setupLayouts()
        populateProfiles() // finales Befüllen des STRUCT
        collectionView.reloadData()
        
        self.collectionView.backgroundView = imageView //Hintergrundbild einfügen
    }

    
// MARK: Rotation - Resize-Cells & Transition
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(
            alongsideTransition: { _ in self.collectionView.collectionViewLayout.invalidateLayout() },
            completion: { _ in }
        )
    }
    
    private func setupViews() {
        view.backgroundColor = .white
        view.addSubview(collectionView)

        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(ProfileCell.self, forCellWithReuseIdentifier: ProfileCell.identifier)
        collectionView.contentInsetAdjustmentBehavior = .never // Cells starten weiter oben
    }

// MARK: Constraints collectionView
    private func setupLayouts() {
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: view.topAnchor),       //view.safeAreaLayoutGuide.topAnchor),
            collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor), //view.safeAreaLayoutGuide.bottomAnchor),
            collectionView.leftAnchor.constraint(equalTo: view.leftAnchor),
            collectionView.rightAnchor.constraint(equalTo: view.rightAnchor)
        ])
    }

    init() {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return profiles.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ProfileCell.identifier, for: indexPath) as! ProfileCell

        let profile = profiles[indexPath.row]
        cell.setup(with: profile)
        cell.contentView.backgroundColor = .red
        return cell
    }
}

extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let width = itemWidth(for: view.frame.width, spacing: LayoutConstant.spacing)

        return CGSize(width: width, height: LayoutConstant.itemHeight)
    }

    func itemWidth(for width: CGFloat, spacing: CGFloat) -> CGFloat {
        let itemsInRow: CGFloat = 2

        let totalSpacing: CGFloat = 2 * spacing + (itemsInRow - 1) * spacing
        let finalWidth = (width - totalSpacing) / itemsInRow

        return floor(finalWidth)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: LayoutConstant.spacing, left: LayoutConstant.spacing, bottom: LayoutConstant.spacing, right: LayoutConstant.spacing)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return LayoutConstant.spacing
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return LayoutConstant.spacing
    }
}

有人可以帮忙吗?

这可能是一个导航栏,最初在 iOS 15 中不可见,但当内容在下方滚动时变得可见。

您可以通过在 viewDidLoadviewDidAppear 中添加此隐藏导航栏:

navigationController?.setNavigationBarHidden(true, animated: false)