有没有一种简单的方法可以在不使用图像的情况下更改UIBarButtonItem的文本颜色?

skå*_*fan 0 iphone cocoa-touch uibarbuttonitem ios4

是否有一种简单的方法来更改UIBarButtonItem的文本颜色而不使用UIImage作为背景?

默认情况下,文本始终为白色.我想让它变黑.

Ilk*_*aci 6

感谢IOS 5.0,您不需要使用图像.您可以使用以下代码设置各种文本属性.

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Title"     style:UIBarButtonItemStyleBordered target:nil action:nil];

if ([button respondsToSelector:@selector(setTitleTextAttributes:forState:)]) {

    NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                               kTextColor,UITextAttributeTextColor,
                                               nil];

    [[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes                   
                                  forState:UIControlStateNormal];

}
Run Code Online (Sandbox Code Playgroud)