无法更改导航项后退按钮的文本和文本颜色

发布于 2024-11-26 04:11:52 字数 720 浏览 0 评论 0原文

我正在尝试使用下面的代码设置导航栏的后退按钮文本颜色(为红色)。 (注意:下面的代码已经在“上一个”视图控制器中,在本例中是在“流行”视图控制器中:

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

enter image description here

How can I resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

悲歌长辞 2024-12-03 04:11:53

我相信只有 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.

最后的乘客 2024-12-03 04:11:53

我只是组合了一个简单的 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 some willAppear/willDisappear logic to animate the back button the way the UINavigationController does while using the leftBarButtonItem property.

https://github.com/typeoneerror/BBCustomBackButtonViewController

|煩躁 2024-12-03 04:11:53

这对我有用。

使用它,您可以更改所有导航按钮的颜色:

[[UIBarButtonItem Appearance] setTintColor:[UIColor redColor]];

将 redColor 替换为以下内容以调整按钮的颜色:

colorWithRed:0/255.0 green:144/255.0 blue:200/255.0 alpha:1.0// pick your color using this

请务必将其放入推送的视图控制器中。不是您想要看到此后退按钮颜色的视图控制器。就像罗宾解释的那样。

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:

colorWithRed:0/255.0 green:144/255.0 blue:200/255.0 alpha:1.0// pick your color using this

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.

等待我真够勒 2024-12-03 04:11:53

我发现全局或本地设置它的最佳方法是

    [[UIBarItem appearance] setTitleTextAttributes:
         [NSDictionary dictionaryWithObjectsAndKeys:
         [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor, 
         [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor, 
         [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
         [UIFont fontWithName:@"AmericanTypewriter" size:0.0], UITextAttributeFont, 
      nil] 
        forState:UIControlStateNormal];

Best way I found to set it globally or locally is

    [[UIBarItem appearance] setTitleTextAttributes:
         [NSDictionary dictionaryWithObjectsAndKeys:
         [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor, 
         [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor, 
         [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
         [UIFont fontWithName:@"AmericanTypewriter" size:0.0], UITextAttributeFont, 
      nil] 
        forState:UIControlStateNormal];
北凤男飞 2024-12-03 04:11:53

问题是你没有一个好的方法来在全局级别设置后退按钮的字体和颜色。这是当前的 iOS 5 中的情况。 为此,我使用 typeoneerror 的 https://github.com/typeoneerror/BBCustomBackButtonViewController。

然而,有一个好方法可以在全局级别设置导航栏按钮的文本和色调颜色属性,而不会破坏其他按钮和菜单的外观。我使用的代码如下:

[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleColor:[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.75] forState:UIControlStateNormal];
[[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] titleLabel] setShadowOffset:CGSizeMake(1.0, 1.0)];

在我的一个项目中。我从 - (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:

[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleColor:[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.75] forState:UIControlStateNormal];
[[[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] titleLabel] setShadowOffset:CGSizeMake(1.0, 1.0)];

In one of my projects. I'm calling it from - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

流年里的时光 2024-12-03 04:11:53
self.navigationItem.backBarButtonItem.tintColor = [UIColor blackColor];
self.navigationItem.backBarButtonItem.tintColor = [UIColor blackColor];
古镇旧梦 2024-12-03 04:11:52

那是因为 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文