Tal Zion
Tal Zion

Reputation: 6526

UINavigation set back button image instead of the <Back text Swift 2.0

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

Upvotes: 4

Views: 4619

Answers (4)

korgx9
korgx9

Reputation: 1264

Swift 5 version

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -80.0), for: .default)

Upvotes: -1

Tanya Berezovsky
Tanya Berezovsky

Reputation: 91

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)

Upvotes: 1

Atef
Atef

Reputation: 2902

Just added UIBarButtonItem and linked it to action.

    @IBAction func backTapped(sender: AnyObject) {
    self.navigationController?.popViewControllerAnimated(true)
}

Upvotes: -1

Sanandiya Vipul
Sanandiya Vipul

Reputation: 764

  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];

Upvotes: 5

Related Questions