How to remove title from UIBarButtonItem.appearance() on iOS 10.3

345 Views Asked by At

I want to personalize the Back Button of my app to have a consistent looking. So I'm setting a image as the Back Button of a Navigation Bar.

The code bellow works fine on any iOS above iOS 10. So, I'm trying to make it work on iOS 10.3.1.

This is basically all the code, that I'm setting on AppDelegate didFinishLaunchingWithOptions

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        applyNavigationAppearances()
        return true
    }

    private func applyNavigationAppearances() {
        let navigationAppearance = UINavigationBar.appearance()
        navigationAppearance.barTintColor = .white
        navigationAppearance.barStyle = .black
        navigationAppearance.backIndicatorImage = UIImage()
        navigationAppearance.backIndicatorTransitionMaskImage = UIImage()
        navigationAppearance.tintColor = .gray

        navigationAppearance.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor.gray,
            NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 20)
        ]

        let backImage = UIImage(named: "chevron-orange-left")
        let backImageUIEdgeInsets = UIEdgeInsets(top: -4, left: -8, bottom: -2, right: 8)
        let backImageWithAlignmentRectInsets = backImage?.withAlignmentRectInsets(backImageUIEdgeInsets)
        let barButtonAppearance = UIBarButtonItem.appearance()

        barButtonAppearance.setBackButtonBackgroundVerticalPositionAdjustment(-1.0, for: .default)
        barButtonAppearance.setBackButtonBackgroundImage(backImageWithAlignmentRectInsets, for: .normal, barMetrics: .default)
    }

This is what I want and get on any iOS above 10:

image This is what I get on iOS 10.3.1:

image Edit 1: It's worth saying this is a big app. A solution that didn't involved changing every ViewController would be ideal.

Edit 2: Thank you for answer guys. However, all solutions posted here are distorting the image shorter or longer, so they don't quite solve my problem.

1

There are 1 best solutions below

1
Eugene Lezov On

You have:

  let backImage = UIImage(named: "icon")
  let backImageUIEdgeInsets = UIEdgeInsets(top: -4, left: -8, bottom: -2, right: 8)
  let backImageWithAlignmentRectInsets = backImage?.withAlignmentRectInsets(backImageUIEdgeInsets)
  appearance.setBackButtonBackgroundVerticalPositionAdjustment(-1.0, for: .default)
  appearance.setBackButtonBackgroundImage(backImageWithAlignmentRectInsets,
                                          for: .normal,
                                          barMetrics: .default)

enter image description here

After that you can add :

 appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear],
                                   for: .normal)
 appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear],
                                   for: .highlighted)

enter image description here

I check this on iPhone 7 10.3.1