-1

I am a newbie in swift so I want to remove a vertical gap between two cells, what should I do to remove that? Also, I want to set an image logo in the navigation bar and tried this code:

let logo = UIImage(named: "Park_Cinema_logo")
         let imageView = UIImageView(image: logo)
         imageView.contentMode = .scaleAspectFit 
         self.navigationItem.titleView = imageView

but it's not working.

Here's the screenshot

  • [Settings spacing between cells](https://stackoverflow.com/questions/13970950/uicollectionview-spacing-margins) – neskafesha Dec 05 '22 at 22:17

1 Answers1

0

To answer the first question about cell spacing: Extend your ViewController using the UICollectionViewDelegateFlowLayout protocol, and implement the below stubs:

extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(
        _ collectionView: UICollectionView, 
        layout collectionViewLayout: UICollectionViewLayout, 
        minimumLineSpacingForSectionAt section: Int
    ) -> CGFloat { 10.0 } // change value to zero if you want to remove spacing.
    
   func collectionView(
       _ collectionView: UICollectionView, 
       layout collectionViewLayout: UICollectionViewLayout, 
       minimumInteritemSpacingForSectionAt section: Int
   ) -> CGFloat { 10.0 } // change value to zero if you want to remove spacing.
}

The second question: Your code looks correct, it could be that the string "Park_Cinema_logo" does not match the image name in your bundle? But if that is not the case here is an extension:
Add this extension in your project.

extension UINavigationBar {
    func configureNavigationBarWithImage(name: String) {
        let navigationBarIconImageView = UIImageView()
        NSLayoutConstraint.activate([
            navigationBarIconImageView.widthAnchor.constraint(equalToConstant: frame.size.width),
            navigationBarIconImageView.heightAnchor.constraint(equalToConstant: frame.size.height)
        ])
        navigationBarIconImageView.contentMode = .scaleAspectFit
        navigationBarIconImageView.image = UIImage(named: name)
        topItem?.titleView = navigationBarIconImageView
    }
}

And in your ViewController:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?
            .navigationBar
            .configureNavigationBarWithImage(name: "Park_Cinema_logo")
    }