问题
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
回答1:
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:
回答2:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"<", style:.Plain, target:nil, action:nil)
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/39366716/backbarbutton-wont-display-correct-title