標籤:
(1)情景:在iOS8.1中,我們通常會利用如下語句,設定全域的導航條按鈕item的主題
UIBarButtonItem *item=[UIBarButtonItem appearance];NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];textAttrs[NSForegroundColorAttributeName]=[UIColor orangeColor];[item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];[item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted]; NSMutableDictionary *dTextAttrs=[NSMutableDictionary dictionaryWithDictionary:textAttrs];dTextAttrs[NSForegroundColorAttributeName]=[UIColor grayColor];[item setTitleTextAttributes:dTextAttrs forState:UIControlStateDisabled];
(2)問題是,我們在上面明明設定了item各種狀態下的屬性(normal,highlighted和disabled),但是當我們在某一個控制器中添加了一個item時,並且設定為disabled狀態時,卻發現不起作用。
-(void)setupNavBar{ self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"發送" style:UIBarButtonItemStyleDone target:self action:@selector(send)]; self.navigationItem.rightBarButtonItem.enabled=NO;}
(3)解決方案
將上面的設定為disabled的語句放置在viewWillAppear中。
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //如果把下面這一句寫在ViewDidLoad中,disabled的item顏色沒有效果 self.navigationItem.rightBarButtonItem.enabled=NO;}
(4)至於為什麼會是這樣?說實話,我也不是很清楚,之前以為調用順序的原因(測試順序正常),後來覺得是viewDidLoad中enabled未賦值(但測試是0,有賦值)。有明白的還請指教。暫且認為是iOS8.1的一個bug吧。在iOS7.1中測試是正常的。
iOS開發解決:iOS8.1中UIBarButtonItem的setTitleTextAttributes對Disabled顏色設定無效問題