0

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)

Taazar
  • 1,545
  • 18
  • 27
Dani
  • 11
  • 5

3 Answers3

2

You can set multiple left bar button items by acessing:

navigationItem.leftBarButtonItems = [barItem1, barItem2]

Same with the right side. :)

Sky Shadow
  • 324
  • 2
  • 10
  • I've actually done that and there's a huge gap in between (its another view controller but ive tried this approach) – Dani Feb 06 '20 at 09:41
  • https://stackoverflow.com/a/32473989/11138557 you can try this one for the spacing issue. – Sky Shadow Feb 06 '20 at 09:48
0

UINavigationItem has a property named leftBarButtonItems.

Put your logo imageView into the customView property of a UIBarItem and size it correctly and you should be all set. Just add it to the the array along with the hamburger button.

edited to add -- try using a custom view, that is:

if let logo = UIImage(named: "logo") {

  let imageView = UIImageView(image:logo)

  let karambaButton = UIBarButtonItem(customView: imageView)

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

  navigationItem.leftBarButtonItems = [backBTN, logoButton]

}
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Can you please see the updated question. I've added information regarding your suggestion. It still doesnt work – Dani Feb 06 '20 at 09:48
0

You can also set auto layout programmatically.

    imageView.translatesAutoresizingMaskIntoConstraints = false
    let horizontalConstraint = imageView.leadingAnchor.constraint(equalTo: hamburgerButton.trailingAnchor, constant: 10)
    let verticalConstraint = newView.centerYAnchor.constraint(equalTo: hamburgerButton.centerYAnchor)
    let widthConstraint = newView.widthAnchor.constraint(equalToConstant: 120)
    let heightConstraint = newView.heightAnchor.constraint(equalToConstant: 120)
    imageView.addConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
EmreSURK
  • 419
  • 3
  • 13