(1)情景:在iOS8.1中,咱們一般會利用以下語句,設置全局的導航條按鈕item的主題app
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)解決方案 code
將上面的設置爲disabled的語句放置在viewWillAppear中。orm
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //若是把下面這一句寫在ViewDidLoad中,disabled的item顏色沒有效果 self.navigationItem.rightBarButtonItem.enabled=NO; }
(4)至於爲何會是這樣?說實話,我也不是很清楚,以前覺得調用順序的緣由(測試順序正常),後來以爲是viewDidLoad中enabled未賦值(但測試是0,有賦值)。有明白的還請指教。暫且認爲是iOS8.1的一個bug吧。在iOS7.1中測試是正常的。get