JVS
JVS

Reputation: 2682

BackBarButton won't display correct title

I am trying to set my backButton to a simple "<" like this:

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

The printing will display "", but running my app on the simulator will always display the title of the previous VC.

Though I tested

 navigationController?.navigationBar.tintColor = UIColor(red:0.60, green:0.60, blue:0.60, alpha:1.0)

will change the button's color.

Note:

I am pushing from a UIViewController embedded in a UINavigationController to just a UIViewController

Upvotes: 0

Views: 215

Answers (4)

Kamlesh Shingarakhiya
Kamlesh Shingarakhiya

Reputation: 2777

Try this

Select your Main.storyboard -> Click Hide Document Outline-> Choose your UIViewController-> Select Navigation Item -> Select Show the attributes inspector-> Back button textfield with one space Back button = " "

OR

EDITED

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

hope this helps.

Upvotes: 0

Gokul G
Gokul G

Reputation: 2096

You need to hide the backbarbutton title through out your app,right?

  • Then this trick may help you in achieving that.

    Swift:

    UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffsetMake(0, -100), forBarMetrics: UIBarMetrics.Default)
    

    Objective C:

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -100)
                                                     forBarMetrics:UIBarMetricsDefault];
    

Add the above code in your appdelegate didFinishLaunchingWithOptions,we are pushing the title out of the frame(hidden) :p.

Result:

enter image description here

Upvotes: 1

Govind Gupta
Govind Gupta

Reputation: 31

In objective c use following code in pushing controller:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"   " style:UIBarButtonItemStylePlain target:nil action:nil];                                                     
[self.navigationItem setBackBarButtonItem:backButton];
self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:ARROW_BACK_ICON];

Do not use "" title for back button use " " title for back button.

Upvotes: 0

Sofeda
Sofeda

Reputation: 1371

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

Upvotes: 0

Related Questions