更改所有UIViewControllers的BackBarButtonItem? [英] Change BackBarButtonItem for All UIViewControllers?

查看:201
本文介绍了更改所有UIViewControllers的BackBarButtonItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS应用中的所有视图上更改BackBarButtonItem,以使它们仅显示后退箭头而没有文本.

I would like to change the BackBarButtonItem on all the views in my iOS app so that they show just the back arrow with no text.

完成此操作的唯一方法是遍历每个UIViewController并设置self.navigationItem.backBarButtonItem.title吗?似乎我应该能够创建一个超类来定义backBarButtonItem.title的默认值,但是我不确定从哪里开始.

Is the only way to accomplish this is to go through every UIViewController and set the self.navigationItem.backBarButtonItem.title ? Seems I should be able to create a superclass to define the default value of backBarButtonItem.title, but I am not sure where to start.

谢谢您的帮助.

推荐答案

您不需要在每个视图控制器类上都进行更改.您可以订阅它并进行一次更改,也可以在应用程序委托中完成它.

You don't need to change it on every view controller class. You can either sublcass it and change once, or you can do it in your app delegate.

如果要对导航栏进行子类化,可以简单地将后退按钮设置为文本@":

If you want to subclass the Navigation Bar, you can simple set the back button with text @"":

  UIBarButtonItem *newBackButton = 
  [[UIBarButtonItem alloc] initWithTitle:@"" 
                                     style:UIBarButtonItemStylePlain 
                                    target:nil 
                                    action:nil];
  [[self navigationItem] setBackBarButtonItem:newBackButton];

如果要在应用程序委托中执行此操作,则可以通过两种方式进行:

If you want to do it in the app delegate you can do it in two ways:

一种方法是将按钮文本颜色设置为透明颜色:

One way is to set the button text color as clear color:

[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor clearColor]} forState:UIControlStateNormal];

另一种方法是将后退按钮放置在远离屏幕的位置,以使用户看不到它.

Another way is to position the back button away from the screen, so user can't see it

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -200) forBarMetrics:UIBarMetricsDefault];

这篇关于更改所有UIViewControllers的BackBarButtonItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆