4

I am trying to change the Back button within the UINavigationController. This is what I tried so far and the output:

1

Code within the ViewDidLoad

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "navBarBackButton")
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "navBarBackButton")
    self.navigationItem.backBarButtonItem?.image = UIImage(named: "navBarBackButton")

Output:

Nav Back Button

I tried to add the below but id didn't help:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.Plain, target:nil, action:nil)

2

Code within the AppDelegate

let backImg: UIImage = UIImage(named: "navBarBackButton")!
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

Output:

Nav Back Button

3

I tried to add the back button in Storyboard but nothing changed

Nav Back Button

4 - Thanks to sanandiya vipul. Still need it to be instead of the 'Back'. This only deletes the 'Back but keeps the image lower right side instead of the entire '

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), forBarMetrics: .Default)

This is the output:

Back Button

This is the result I am hoping to achieve

Back Button

Tal Zion
  • 6,308
  • 3
  • 50
  • 73

4 Answers4

5
  1. Below Code add your ViewDidLoad method. its work for you fine.

for Swift

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), forBarMetrics: .Default)

for Objective-c

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0,- 80.f) forBarMetrics:UIBarMetricsDefault];

Sanandiya Vipul
  • 764
  • 4
  • 16
1

Swift 4 version

Set Back title text color to clear

let barButtonItemAppearance = UIBarButtonItem.appearance()        
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)                
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)
Krunal
  • 77,632
  • 48
  • 245
  • 261
-1

Just added UIBarButtonItem and linked it to action.

    @IBAction func backTapped(sender: AnyObject) {
    self.navigationController?.popViewControllerAnimated(true)
}
Atef
  • 2,872
  • 1
  • 36
  • 32
-1

Swift 5 version

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -80.0), for: .default)
korgx9
  • 1,234
  • 22
  • 30