如何在 UINavigationController 标题视图中自定义后退按钮的文本颜色?

发布于 2024-07-10 14:11:25 字数 922 浏览 13 评论 0原文

我在 UINavigationController 的导航栏上使用自定义 tintColor,由于颜色太浅,我需要使用深色文本。 交换标题视图和我在右侧添加的自定义按钮相对容易,但我似乎无法将自定义视图粘贴到后退按钮上。 这就是我现在正在尝试的:

UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectZero];

[backLabel setFont:[UIFont fontWithName:[[UIFont fontNamesForFamilyName:@"Arial Rounded MT Bold"] objectAtIndex:0] size:24.0]];
[backLabel setTextColor:[UIColor blackColor]];
[backLabel setShadowColor:[UIColor clearColor]];

[backLabel setText:[aCategory displayName]];
[backLabel sizeToFit];
[backLabel setBackgroundColor:[UIColor clearColor]];

UIBarButtonItem *temporaryBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:backLabel];

temporaryBarButtonItem.customView = backLabel;
[backLabel release];

self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];]

自定义视图并没有坚持下去,而且我没有看到任何明显简单的方法来获取默认按钮内的实际文本并开始更改其样式。

I'm using a custom tintColor on my UINavigationController's navigation bar, and because the color is so light I need to use dark colored text. It's relatively easy to swap out the title view, and the custom buttons I've added on the right hand side, but I can't seem to get a custom view to stick on the back button. This is what I'm trying right now:

UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectZero];

[backLabel setFont:[UIFont fontWithName:[[UIFont fontNamesForFamilyName:@"Arial Rounded MT Bold"] objectAtIndex:0] size:24.0]];
[backLabel setTextColor:[UIColor blackColor]];
[backLabel setShadowColor:[UIColor clearColor]];

[backLabel setText:[aCategory displayName]];
[backLabel sizeToFit];
[backLabel setBackgroundColor:[UIColor clearColor]];

UIBarButtonItem *temporaryBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:backLabel];

temporaryBarButtonItem.customView = backLabel;
[backLabel release];

self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];]

The custom view doesn't stick though, and I don't see any obviously easy way to get at the actual text inside the default button and start changing its style.

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

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

发布评论

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

评论(5

耶耶耶 2024-07-17 14:11:25

这有效,解决了最初的问题(更改导航栏“后退”按钮 - 其他工具栏上没有按钮,选项卡栏上没有按钮等):

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor,nil]
         forState:UIControlStateNormal];

This works, solves the original question (change the Navbar BACK button - and no buttons on other toolbars, no buttons on tab bars, etc):

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor,nil]
         forState:UIControlStateNormal];
秋风の叶未落 2024-07-17 14:11:25

没有官方的方法可以做到这一点。 我能想到的两种方法是:

  • 导航视图树以找到现有的后退按钮,并手动设置其文本颜色。 这可能不是一个好主意,因为它很脆弱,并且该按钮甚至可能没有可配置的 textColor 属性。

  • 创建您自己的后退按钮(即,使用您自己的图像),并设置其颜色。 这就是我们在很多地方所做的事情。 这需要更多的工作,但结果正是您想要的。

There's no official way to do this. The two methods that I could think of are:

  • navigate the view tree to find the existing Back button, and set its text color manually. This is probably not a good idea, as it's fragile, and the button may not even have a configurable textColor property.

  • create your own back button (ie, with your own image), and set its color. This is what we do in a number of places. It's a little more work, but the results are exactly what you want.

没有心的人 2024-07-17 14:11:25

我尝试遍历来设置文本,但它最终出现在 UINavigationItemButtonView 上,它似乎只是直接绘制。 没有办法设置颜色。

我能够让它工作的唯一方法是将文本转换为图像,然后将后退按钮设置为图像。

UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:nil action:nil];
navItem.backBarButtonItem = backItem;
[backItem release];

I tried traversing to set the text, but it ends ups on a UINavigationItemButtonView, which appears to just draw directly. There is no way to set the color.

The only way I was able to get it to work is to convert the text into an image, and then set the back button to the image.

UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:nil action:nil];
navItem.backBarButtonItem = backItem;
[backItem release];
还不是爱你 2024-07-17 14:11:25

SketchV 是正确的。 在以前版本的 SDK 中,他们使用不同的 UIButton 实例,允许通过遍历子视图层次结构进行自定义。 这不再可能了。

SkotchV is correct. In previous versions of the SDK, they used distinct UIButton instances, which allowed customizing by traversing the subview hierarchy. It is no longer possible.

山川志 2024-07-17 14:11:25

@米基劳尔博士
在我的开发环境中可以迭代层次结构来设置文本颜色。
当您填充 UINavigationBar 的左视图和右视图时,您的 UINavigationBar 将有 UIButtons 作为子视图。 如果您想将 UIToolbars 设为导航栏的父级,则可以迭代 navBar.items,检查 UINavigationItems(通过 isKindOfClass:)。 这些导航项具有左项数组和右项数组。 这些数组包含(除其他外)您放置在导航栏左视图和右视图中的工具栏。 这些左侧和右侧数组使您可以访问工具栏的 UIBarButtonItems。 然后,您可以在 UIBarButtonItems 上调用 setTitleTextAttributes 来设置其文本和阴影颜色。

但是,正如本·戈特利布所说,这是一种脆弱的方法。 这让你想知道导航栏的组织将来是否会改变。

@DrMickeyLauer
Iterating the hierarchy to set the text color is possible in my development environment.
When you populate your UINavigationBar's left and right views, your UINavigationBar will have UIButtons as subviews. If you're getting fancy and parenting UIToolbars to your navigation bar, you can iterate through your navBar.items, checking for UINavigationItems (via isKindOfClass:). These navigation items have left and right item arrays. These arrays contain (among other things) the toolbar(s) you put in the navigation bar's left and right views. These left and right arrays give you access to the toolbar's UIBarButtonItems. You can then call setTitleTextAttributes on the UIBarButtonItems to set their text and shadow colors.

But, as Ben Gottlieb stated, this is a fragile approach. It leaves you wondering whether the navigation bar organization will change in the future.

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