简体   繁体   English

iOS如何在整个应用程序中更改导航项的“后退”按钮文本

[英]iOS How to change the “Back” button's text of the Navigation Item throughout the app

Background: I've setup my app using this link . 背景:我已经使用此链接设置了我的应用。

This is because my app needs to change the locale on-the-fly, instead of referring to the phone's language settings. 这是因为我的应用程序需要即时更改语言环境,而不是参考手机的语言设置。

Have a look at this screenshot (Pardon me, I don't have enough reputation points to upload the photo here). 看一下这个屏幕截图 (对不起,我没有足够的声誉点可以在此处上传照片)。 Right now, the language being displayed is Chinese. 现在,显示的语言是中文。 However, the "Back" button on the top left is in the language set in the phone's settings. 但是,左上角的“返回”按钮使用手机设置中设置的语言。

What I know now: 我现在知道的是:

I am able to set the text in the "Back" by setting it in the previous view ie the view before this one is pushed. 我可以通过在上一个视图中(即在按下此视图之前的视图中)设置“后退”中的文本。

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"<MY CUSTOM BACK>" style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backItem];

This would work, I'll be able to set the text to what I want. 这将起作用,我将能够将文本设置为所需的文本。 But this means I would need to add this code snippet in almost every view controllers. 但这意味着我几乎需要在每个视图控制器中添加此代码段。

My question: I want to be able to set it in one location, and then it will apply throughout the app. 我的问题:我希望能够将其设置在一个位置,然后将其应用于整个应用程序。 I wonder if it is possible and if so, how can I achieve it? 我想知道是否有可能,如果可以,我如何实现呢?

Thanks in advance! 提前致谢!

从实现了所有设计/通用方法的BaseViewController子类化ViewControllers,或将NavigationController子类化并在情节提要中更改类。

In general, I think this goes against Apple's Design Guidelines. 总的来说,我认为这违反了苹果的设计准则。

However, this post seems to provide a solution. 但是,此帖子似乎提供了解决方案。

Modify global UI customiztions in your AppDelegate's didFinishLaounchingWithOptions method, 在AppDelegate的didFinishLaounchingWithOptions方法中修改全局UI自定义设置,

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
     [self customizeUI];
 }

.

 -(void)customizeUI
{
    [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                          [UIColor redColor],NSForegroundColorAttributeName,
                                                          [UIFont fontWithName:@"Arial" size:20.0],NSFontAttributeName,
                                                          nil]
                                                forState:UIControlStateNormal];
}

You can use Aspects like this: 您可以像这样使用Aspects

[UIViewController aspect_hookSelector:@selector(viewDidLoad) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo){
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
    [((UIViewController*)aspectInfo.instance).navigationItem setBackBarButtonItem:backItem];
} error:NULL];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM