2015-06-14 112 views
1

我已经创建了三个条形按钮项目的UIToolBar。当使用默认字体和字体大小时,所有效果都很好。但是,字体有点太大,我想调整它的大小。我使用以下代码成功地在正常状态下自定义字体大小和颜色,但是当禁用按钮#1时,对于启用和禁用状态,文本颜色都保持黑色 - 即它不会将颜色更改为灰色。无法更改禁用状态下的UIBarButtonItem颜色属性

NSDictionary *ena = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:12.0], NSForegroundColorAttributeName: 
          [UIColor blackColor]}; 
NSDictionary *dis = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:12.0], NSForegroundColorAttributeName: 
          [UIColor grayColor]}; 

[[UIBarButtonItem appearance] setTitleTextAttributes:ena forState:UIControlStateNormal]; 
[[UIBarButtonItem appearance] setTitleTextAttributes:dis forState:UIControlStateDisabled]; 

我很感激我对上述代码出错的一些反馈。

回答

-1

为barButtonItem创建出口;假设它是myBarButton。最初从故事板中禁用它。然后在viewDidLoad中称之为()

if (myBarButton.enabled == 'NO') { 
    NSDictionary *dis = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:12.0], NSForegroundColorAttributeName: 
           [UIColor grayColor]}; 
    [[UIBarButtonItem appearance] setTitleTextAttributes:ena forState:UIControlStateNormal]; 
} 

然后在必要时启用myBarButton做了一些工作之后,再运行下面

NSDictionary *dis = @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:12.0], NSForegroundColorAttributeName: 
          [UIColor blackColor]}; 
[[UIBarButtonItem appearance] setTitleTextAttributes:ena forState:UIControlStateNormal]; 

其实代码,因为我观察UIControlStateDisabled不能正常工作。