如何本地化UIBarButtonItem? Xcode中

时间:2014-08-14 22:07:21

标签: ios localization uibarbuttonitem

在本地化图像时,我使用以下代码:

 UIImage* localTutorial = [UIImage imageNamed:NSLocalizedString(@"tutorial.png", nil)];

我想本地化UIBarButtonItem并根据本地化字符串更改它的标题,我该如何实现。提前谢谢。

3 个答案:

答案 0 :(得分:1)

你做错了,按以下方式使用的方法

 NSString *barButtonItemTitle = NSLocalizedString(@"Button title here", @"")
 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:barButtonItemTitle
            style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

答案 1 :(得分:0)

您可以使用

- (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

答案 2 :(得分:0)

快捷键:

// 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.")
}