如何本地化UIBarButtonItem? Xcode中

问题描述 投票:0回答:3

本地化图像时,我使用此代码:

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

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

ios localization uibarbuttonitem
3个回答
1
投票

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

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

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

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.")
}
© www.soinside.com 2019 - 2024. All rights reserved.