2015-06-14 90 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不能正常工作。