Home > Enterprise >  UICollectionview how to avoide top bar by scrolling
UICollectionview how to avoide top bar by scrolling

Time:10-22

When I start my App everything is fine.

As it should stay.

But as soon I start to scroll down a strange Bar at the Top appears. Strange Bar by Scrolling down

I looked for some frame issue but I can't find one. I have no Idea what causes this behavior. I coded my UICollectionview programmatically.

Here is my Code:

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

Can somebody please help?

CodePudding user response:

This is probably a navigation bar that is initially invisible in iOS 15, but becomes visible when content is being scrolled underneath.

You can hide the navigation bar by adding this in viewDidLoad or viewDidAppear :

navigationController?.setNavigationBarHidden(true, animated: false)
  • Related