iOS開發解決:iOS8.1中UIBarButtonItem的setTitleTextAttributes對Disabled顏色設定無效問題

來源:互聯網
上載者:User

標籤:

(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顏色設定無效問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.