1

In my app, I have a custom icon for going back to previous ViewController. It was working perfectly, but with iOS 13, the icon is misplaced.

The fun fact is that I still have to click on the left (where the back icon used to be, and in other words, on the blank space) to go back to previous ViewController.

Do you have any idea why?

Here is the code I use to customize the back button.

var back = UIImage(named: "backButton")
back = back?.withAlignmentRectInsets(UIEdgeInsetsMake(0, -50, 0, 50))
self.navigationController?.navigationBar.backIndicatorImage = back?.withAlignmentRectInsets(UIEdgeInsetsMake(0, -50, 0, 50))

self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = back?.withAlignmentRectInsets(UIEdgeInsetsMake(0, -50, 0, 50))

self.navigationController?.navigationBar.backItem?.backBarButtonItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.plain, target: nil, action: nil)

self.navigationController?.navigationBar.tintColor = .black

And this is a screenshot of the back icon, not on the left anymore: enter image description here

Amit
  • 4,837
  • 5
  • 31
  • 46
KevinB
  • 2,454
  • 3
  • 25
  • 49

1 Answers1

0

Using UIBarButtonItem with customView suits well to your situation. By creating an instance of UIButton gives you command over choosing image and title both, besides custom selector can also be passed to handle button events.

Here is sample code:

let button = UIButton(type: .system)
button.setImage(UIImage(named: "YOUR IMAGE NAME"), for: .normal)
button.setTitle("TITLE IF ANY", for: .normal)
button.sizeToFit()
button.addTarget(self, action: #selector(backBtnPressed(sender:)), for: .touchUpInside)

let leftBtn = UIBarButtonItem(customView: button)
leftBtn.style = .plain
navigationItem.leftBarButtonItem = leftBtn

@objc func backBtnPressed(sender: UIBarButtonItem) {
    print("BackBtnPressed")
}
Aamir
  • 16,329
  • 10
  • 59
  • 65