无法更改导航项后退按钮的文本和文本颜色
我正在尝试使用下面的代码设置导航栏的后退按钮文本颜色(为红色)。 (注意:下面的代码已经在“上一个”视图控制器中,在本例中是在“流行”视图控制器中:
//Customize Back button
UIView *backButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
UILabel *backButtonLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
backButtonLabel.text = @"back";
backButtonLabel.textColor = [UIColor colorWithHexString:@"cf212a"];
[backButtonView addSubview:backButtonLabel];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithCustomView:backButtonView];
self.navigationItem.backBarButtonItem = backButton;
但是,我没有得到我想要的效果。我仍然在后退按钮中获得默认的白色文本文本也没有更改为“后退”
如何解决此问题?
I am trying to set the backbutton text color (to red) of my navbar using the code below. (NOTE: The code below is already in the 'previous' view controller, in this case in the 'Popular' view controller:
//Customize Back button
UIView *backButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
UILabel *backButtonLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 40)];
backButtonLabel.text = @"back";
backButtonLabel.textColor = [UIColor colorWithHexString:@"cf212a"];
[backButtonView addSubview:backButtonLabel];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithCustomView:backButtonView];
self.navigationItem.backBarButtonItem = backButton;
However, I am not getting the effect I want. I am still getting the default white color text in the backbutton. The text is not changed to 'back' as well
How can I resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我相信只有
title
属性在后退按钮项中很重要;它可以让您在中间有一个长标题,在后退按钮中有一个较短的标题。添加自定义“后退按钮”的最简单方法是设置
UINavigationItem.leftBarButtonItem
;您必须适当地设置其目标/操作。I believe that only the
title
property matters in the back button item; it is there to let you have a long title in the middle and a shorter title in the back button.The easiest way to add a custom "back button" is to set
UINavigationItem.leftBarButtonItem
; you'll have to set its target/action appropriately.我只是组合了一个简单的 UIViewController 子类,它添加了一个可自定义的后退按钮,允许您更改文本颜色。它基本上添加了一些
willAppear
/willDisappear
逻辑来为后退按钮设置动画,就像UINavigationController
在使用leftBarButtonItem
时所做的那样财产。https://github.com/typeoneerror/BBCustomBackButtonViewController
I just put together a simple
UIViewController
subclass that adds a customizable back button that allows you to change text colors. It basically adds somewillAppear
/willDisappear
logic to animate the back button the way theUINavigationController
does while using theleftBarButtonItem
property.https://github.com/typeoneerror/BBCustomBackButtonViewController
这对我有用。
使用它,您可以更改所有导航按钮的颜色:
[[UIBarButtonItem Appearance] setTintColor:[UIColor redColor]];
将 redColor 替换为以下内容以调整按钮的颜色:
请务必将其放入推送的视图控制器中。不是您想要看到此后退按钮颜色的视图控制器。就像罗宾解释的那样。
This worked for me.
Using this you can change the color of all of the navigation buttons:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
Replace redColor with the following to adjust the color of the buttons:
Be sure to put this in the view controller that pushes. Not the view controller where you want to see this back button color. Like Robin explained.
我发现全局或本地设置它的最佳方法是
Best way I found to set it globally or locally is
问题是你没有一个好的方法来在全局级别设置后退按钮的字体和颜色。这是当前的 iOS 5 中的情况。 为此,我使用 typeoneerror 的 https://github.com/typeoneerror/BBCustomBackButtonViewController。
然而,有一个好方法可以在全局级别设置导航栏按钮的文本和色调颜色属性,而不会破坏其他按钮和菜单的外观。我使用的代码如下:
在我的一个项目中。我从
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
调用它The problem is you don't have a good way to set back button's font and colors at global level. That's in the current iOS 5. For that I'm using typeoneerror's https://github.com/typeoneerror/BBCustomBackButtonViewController mentioned here.
However there is a good way to set Navigation Bar buttons' text and tint color properties at global level without destroying other button's and menus appearance. I'm using code like:
In one of my projects. I'm calling it from
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
那是因为 backbuttonitem 是一个 uibarbuttonitem,它将在下一个视图控制器上看到,它将被推送到当前视图控制器的顶部,而不是当前视图控制器的顶部。
如果您想设置当前视图的后退按钮,就像您在上面的代码中尝试执行的那样,那么只需将代码移动到它之前显示的视图控制器(在堆栈中的下方)。这样,当您推送视图时您想要显示在上一个视图控制器中设置的自定义后退按钮的控制器。这是因为后退按钮属于前一个视图控制器,它将推送您的新视图控制器......
Thats because the backbuttonitem is a uibarbuttonitem that will be seen on the next view controller which will be pushed on top of your current view controller and not of the current view controller.
If you want to set the backbutton of the current view like you are trying to do in your above code than just move the code to the view controller that is shown before it(below it in the stack.) so that when you push the view controller for which you want to show custom back button which was set in the previous view controller. This is because the back button belongs to the previous view controller that is gonna push your new view controller...