(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