skip to Main Content

I have set up a navigationBar and I’m trying to add a PNG to the left as a leftBarButtonItem but it’s showing up in the middle of the navigationBar.

private func configureNavBar() {
    var image = UIImage(named: "netflixLogo") // PNG
    image = image?.withRenderingMode(.alwaysOriginal)
    //self.navigationController?.navigationBar.topItem?.leftBarButtonItem = UIBarButtonItem(image: image, style: .done, target: self, action: nil) This didn't work either.
    
    navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .done, target: self, action: nil)
    
    //These buttons are working, they are on the right side.
    navigationItem.rightBarButtonItems = [
        UIBarButtonItem(image: UIImage(systemName: "person"), style: .done, target: self, action: nil),
        UIBarButtonItem(image: UIImage(systemName: "play.rectangle"), style: .done, target: self, action: nil),
    ]
    
    navigationController?.navigationBar.tintColor = .white
}

This is what I see on the simulator:

enter image description here

2

Answers


  1. It’s on the left, but the image is just too wide and tall for the nav bar so is displayed aspect fitted in the center of its bounds

    Login or Signup to reply.
  2. I have same issue.

    Try this:

    extension UIImage {
        func resizeTo(size: CGSize) -> UIImage {
            let renderer = UIGraphicsImageRenderer(size: size)
            let image = renderer.image { _ in
                self.draw(in: CGRect.init(origin: CGPoint.zero, size: size))
            }
            
            return image.withRenderingMode(self.renderingMode)
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search