使用UIAppearance为UIBarButtonItem设置后退按钮的文本颜色?

And*_*son 10 iphone ipad ios

我一直在使用UIAppearance自定义我的UIBarButtonItems,但我不知道如何更改后退按钮的文本样式.这是怎么做到的?

我知道如何设置背景图片:

[[UIBarButtonItem appearance]
   setBackButtonBackgroundImage:[UIImage imageNamed:@"arrow-button-static.png"]
   forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

  [[UIBarButtonItem appearance]
   setBackButtonBackgroundImage:[UIImage imageNamed:@"arrow-button-pressed.png"]
   forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

ric*_*ter 23

您无法仅更改"后退"按钮的文本属性.(或者至少,你不能使用外观代理全局这样做.)Apple显然希望它的文本与导航栏中其他按钮的文本相匹配,只有它的形状(背景图像)不同.

正如@DhruvGoel所说,你可以这样设置文本属性:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [UIColor redColor],UITextAttributeTextColor,
                                       nil];

[[UIBarButtonItem appearance] setTitleTextAttributes:attributes                   
                          forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

或者,对于导航栏中的条形按钮:

    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
            setTitleTextAttributes:attributes                   
                          forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

有关如何在视图/视图控制器层次结构的特定部分中自定义外观的更多详细信息,请参阅UIAppearance协议文档.虽然正如我所说,这不会让你在全球范围内使Back按钮文本与同一导航栏中的其他按钮不同.(当你的视图控制器出现时,你可以直接改变每一个,但这可能不是一个好主意.)

哦,抱歉弹劾事.

  • 对总统的好评 (4认同)
  • 您还可以通过在UINavigationBars中包含的UIImageViews上设置tintColor来更改backIndicatorImage的颜色: (2认同)

Dhr*_*oel 9

要更改后退按钮项文本颜色,可以使用以下命令:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [UIColor redColor],UITextAttributeTextColor,
                                           nil];

[[UIBarButtonItem appearance] setTitleTextAttributes:attributes                   
                              forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

您可以使用UIStringDrawing.h中的键指定文本属性字典中标题的字体,文本颜色,文本阴影颜色和文本阴影偏移,即UITextAttributeFont,UITextAttributeTextColor,UITextAttributeTextShadowColor和UITextAttributeTextShadowOffset

请注意,这只是iOS 5.0以上版本

  • 如果您的VC在导航栏中有按钮以外的按钮,这也会改变它们的外观; 我相信OP(以及美国第17任总统?)正在询问如何改变*仅*后退按钮的外观.但我很确定没有方法 - 尽管它们的灵活性,外观定制API仍然假设您的UI将遵循某些规则,导航按钮中的一致文本样式就是其中之一. (3认同)