0

I am trying to set a custom image for the back bar button. I can remove the text, however, the default chevron arrow is still there. As illustrated below;

I'm on x-code 11.3

enter image description here

My code is;

let chevronImage = UIImage(systemName: "arrow.left")!.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0))
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
navigationController?.navigationBar.backIndicatorImage = chevronImage
navigationController?.navigationBar.backIndicatorTransitionMaskImage = chevronImage
David Henry
  • 1,972
  • 20
  • 43
  • Instead of `backBarButtonItem `, try to change [leftbarbuttonitem](https://developer.apple.com/documentation/uikit/uinavigationitem/1624936-leftbarbuttonitem) – dahiya_boy Dec 18 '19 at 13:29
  • That brings me back to the default backBarButton i.e. icon and title – David Henry Dec 18 '19 at 13:31
  • It will override your back button with the custom button. Check https://stackoverflow.com/questions/43254254/left-bar-button-item – dahiya_boy Dec 18 '19 at 13:33
  • this does work, however, loses the default behaviour of the back barButtonItem. Is this correct? – David Henry Dec 18 '19 at 13:50
  • Working example https://github.com/mattneub/Programming-iOS-Book-Examples/blob/02f14c9e358566d1c755d82de3f90c2924115c7d/bk2ch12p604NavigationBarAppearance/NavigationBarAppearance/TableViewController.swift – matt Dec 18 '19 at 14:21

3 Answers3

1

You could try something like this:

navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "arrow.left"),
                                                   style: .plain,
                                                   target: nil,
                                                   action: nil)
kd02
  • 422
  • 5
  • 14
0

Try create your own custom navigation bar class and use that in viewDidLoad() function

let backButtonBackgroundImage = UIImage(named: "ic_navbar_back")!
let barAppearance = UINavigationBar.appearance(whenContainedInInstancesOf[CustomNavBar.self])
barAppearance.backIndicatorImage = backButtonBackgroundImage
barAppearance.backIndicatorTransitionMaskImage = backButtonBackgroundImage
0

Try this:

self.navigationController?.navigationBar.backIndicatorImage = images self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = images self.navigationController?.navigationBar.tintColor = UIColor.clear

assembler
  • 3,098
  • 12
  • 43
  • 84