为什么无法设置 UIBarButtonItem Disabled状态的颜色

 我来答
盘默M2
2016-04-09 · TA获得超过2.9万个赞
知道大有可为答主
回答量:9723
采纳率:93%
帮助的人:8129万
展开全部
应该使用
javascript[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateDisabled];

参照UIBarItem.h里面的
javascript
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

这两个方法是Appearance protocol的方法。
应该是调用方法的时间点上有问题,这个是我的demo代码片段
javascript- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {
[self setNaviagtionItem];
}

return self;
}

- (instancetype)init
{
self = [super init];

if (self) {
[self setNaviagtionItem];
}

return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];

if (self) {
[self setNaviagtionItem];
}

return self;
}

- (void)setNaviagtionItem
{
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"bb"style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.leftBarButtonItem = leftItem;

NSDictionary* textAttributes = [NSDictionary dictionaryWithObject: [UIColor blackColor]
forKey: NSForegroundColorAttributeName];

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

NSDictionary* textAttributes1 = [NSDictionary dictionaryWithObject: [UIColor brownColor]
forKey: NSForegroundColorAttributeName];
[[UIBarButtonItem appearance] setTitleTextAttributes: textAttributes1
forState: UIControlStateDisabled];

NSDictionary* textAttributes2 = [NSDictionary dictionaryWithObject: [UIColor yellowColor]
forKey: NSForegroundColorAttributeName];
[[UIBarButtonItem appearance] setTitleTextAttributes: textAttributes2
forState: UIControlStateSelected];
}

- (void)viewDidLoad {
[super viewDidLoad];
[self setNaviagtionItem];

self.navigationItem.leftBarButtonItem.enabled = NO;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式