Search code examples
swiftuinavigationcontrolleruinavigationbaruinavigationitem

titleView of Navigation item moved to the left?


I have an app with a left (hamburger) navigationItem.leftBarButtonItem and we've set the titleView to the logo of the company. The problem is that they want the logo next to the hamburger. The hamburger button is set in the Storyboard's ViewController and the logo is programmatically, like this:

let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 120, height: 60)
navigationItem.titleView = imageView

Is there a way to move it to the left?

UPDATE: Regarding the suggestions to use leftBarButtonItems

I've done this in another UIViewController and the result wasn't as I expected it to be. Here's the code:

let logo    = UIImage(named: "logo")!
let karambaButton   = UIBarButtonItem(image: logo,  style: .plain, target: self, action: nil)
let backBTN = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: navigationController, action: #selector(UINavigationController.popViewController(animated:)))

navigationItem.leftBarButtonItems = [backBTN, logoButton]

And here's the result of it:

enter image description here

(The dark block is the image, I had to cover it up because it's a client)


Solution

  • You can set multiple left bar button items by acessing:

    navigationItem.leftBarButtonItems = [barItem1, barItem2]
    

    Same with the right side. :)