Setting a Custom Image for a Back Button on UINavigationBar

前端 未结 8 2178
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 10:55

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

8条回答
  •  一生所求
    2020-12-14 11:22

    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
    

提交回复
热议问题