Home > Software engineering >  CollectionViewController does not show searchbar and navigation bar
CollectionViewController does not show searchbar and navigation bar

Time:11-22

I'm trying to follow along with a video course to implement photo gallery with Unsplash.

First I implemented UIViewController with searchbar and navigation bar in it

PhotoCollectionViewController: UIViewController

and manually adding searchbar and navigation bar

//MARK: - Nav bar inelkaar steken hier

private func setupNavigationBar(){
    let titleLabel = UILabel()
    titleLabel.text = "Photos"
    titleLabel.font = UIFont.systemFont(ofSize: 15)
    titleLabel.textColor = .black
    navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: titleLabel)
    navigationItem.rightBarButtonItems = [addBarButtonItem, actionBarButtonItem]
}

and the same for searchbar

so I got the result as this: enter image description here

after this the PhotoCollectionViewController: UIViewController inheritance seemed be wrong what I did, and I need to change it to UICollectionViewController

I make then another controller inherited from UICollectionViewController with the same logic PhotoCollectionViewController: UICollectionViewController

It compiles without any issues, but I don't see navigation an search bar anymore

enter image description here

How can I fix this?

CodePudding user response:

May be your collectionView is hiding search bar and navigation bar. check collectionView constraints. (specially top anchor)

// PS: I could comment, but I don't have 50 reputation.

CodePudding user response:

maybe you can try this:

  1. Check the top constraint first of your collectionView. And maybe you can add this code to you viewDidLoad() :

    self.navigationController?.setNavigationBarHidden(true, animated: false)

  2. if you use ScollView at fullscreen (SuperView), you can use this :

    scrollView.contentInsetAdjustmentBehavior = .never

  3. for searchBar IBOutlet you can add this :

    searchBar.becomeFirstResponder()

  • Related