本地化图像时,我使用此代码:
UIImage* localTutorial = [UIImage imageNamed:NSLocalizedString(@"tutorial.png", nil)];
我想本地化一个UIBarButtonItem并根据本地化字符串更改它的标题,我该如何实现。提前致谢。
你做错了,方法以下列方式使用
NSString *barButtonItemTitle = NSLocalizedString(@"Button title here", @"")
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:barButtonItemTitle
style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
您可以使用
- (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
要么
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
迅速:
// MARK: - Lifecycle Methods
override func viewDidLoad() {
super.viewDidLoad()
// if it's the back button
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: NSLocalizedString("action_close", comment: ""), style: .plain, target: self, action: #selector(self.didTapMyLocalizedBarButtonItem))
// OR maybe you need the following instead (like I did):
// if this is the root view controller for the current navigation controller (therefore, it's not a back button technically)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("action_close", comment: ""), style: .plain, target: self, action: #selector(self.didTapMyLocalizedBarButtonItem))
}
// MARK: - Custom Button Action
@IBAction func didTapMyLocalizedBarButtonItem(_ sender: Any) {
print("Custom bar button item tapped.")
}