4
    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)

I use above to remove the backButtonTitle prior to iOS 11. But on iOS 11 that is not properly working. Arrow shifts bit downwards. enter image description here How to fix this?

Edit: Other way by erasing Title can solve my problem but my concern is why that old way is not working anymore.

Nitesh
  • 1,564
  • 2
  • 26
  • 53
  • 1
    Possible duplicate of [UINavigationBar Hide back Button Text](https://stackoverflow.com/questions/23853617/uinavigationbar-hide-back-button-text) – Luzo Nov 12 '17 at 11:40
  • @Luzo I have to edit every VCs for that. I don't want to do it by that method. – Nitesh Nov 12 '17 at 11:42
  • @Nitesh You can use common superclass. Or you can subclass `UINavigatonController` and make the magic happen there. Even if you had to edit every VC, it would be better than your original hacky solution. – Sulthan Nov 12 '17 at 12:30
  • See Here, https://stackoverflow.com/a/45651186/6479530 – Rajamohan S Nov 13 '17 at 03:52
  • 1
    This one is much better: UIOffsetMake(-1000, 0) – Ahmadreza Jan 18 '18 at 20:08

4 Answers4

4

The other way is to set UINavigationControllerDelegate for your navigationController and erase the title in its function.

class NoBackButtonTitleNavigationDelegate: UINavigationControllerDelegate {

    func navigationController(
        _ navigationController: UINavigationController,
        willShow viewController: UIViewController,
        animated: Bool
    ) {
        viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
    }
}
Luzo
  • 1,336
  • 10
  • 15
  • Best solution I have seen for that question. – Victor Camargo Dec 06 '17 at 18:47
  • It tells me that: Type 'NoBackButtonTitleNavigationDelegate' does not conform to protocol 'NSObjectProtocol' and then it adds a lot of Method stubs – Nico S. Jan 16 '18 at 15:27
  • Nico, all you have to do is implement the delegate for example: class ViewController: UIViewController, UINavigationControllerDelegate{ override func viewDidLoad(){ super.viewDidLoad() navigationController?.delegate = self } func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil) } } that shuld work – Sergio Trejo Jan 16 '18 at 16:42
0

Use this code,

var newBackButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(self.back))
navigationItem?.leftBarButtonItem = newBackButton 
Maniganda saravanan
  • 2,188
  • 1
  • 19
  • 35
0

Create Navigation controller class as below. Assign this "CustomNavViewController" to UINavigationController in your StoryBoard

class CustomNavViewController: UINavigationController,UINavigationControllerDelegate 
{


override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func navigationController(
    _ navigationController: UINavigationController,
    willShow viewController: UIViewController,
    animated: Bool
    ) {
    viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
}

}

So, no need to do it in every viewControllers. At last remove below line from AppDelegate class if present,

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
Avaan
  • 4,709
  • 1
  • 10
  • 13
0

Instead of moving title vertically you can move horizontally. Also in case the title is long you can make its color transparent. Works just fine me:

let barButtonItemAppearance = UIBarButtonItem.appearance()
    barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
    barButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)
Max Niagolov
  • 684
  • 9
  • 26