I am trying to set a custom image for the back button that automatically gets place onto a navigation bar when a new view is pushed onto the stack.
I have tried add
This code worked for Swift 5 and XCode 10 :
Add this code to AppDelegate's func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
to Apply to all of your view controllers:
//Custom back button
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)
let backImg: UIImage = UIImage(named: "back")!
barButtonItemAppearance.setBackButtonBackgroundImage(backImg, for: .normal, barMetrics: .default)
let image = UIImage()
UINavigationBar.appearance().backIndicatorImage = image
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image