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

Ros*_*her 13 iphone cocoa-touch

我在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];]
Run Code Online (Sandbox Code Playgroud)

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

Ada*_*dam 38

这有效,解决了原始问题(更改Navbar BACK按钮 - 其他工具栏上没有按钮,标签栏上没有按钮等):

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor,nil]
         forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)


Ben*_*ieb 6

没有正式的方法可以做到这一点.我能想到的两种方法是:

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

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