代码的执行顺序如上图所示,去掉
childVC.view.backgroundColor = JXRandomColor;
去掉这行代码,按钮样式就可以正常显示,而加上只会显示UIControlStateNormal 的样式。
我开始以为是因为childVC.view.backgroundColor 提前调用了.enabled = NO,而还没有设置样式。
后来我将这句代码放到[self addChildViewController:nav]; 后。
执行顺序也是先设置样式,再设置.enabled = NO,但disabled 样式设置依然无效。
我输出了几个log 发现如下顺序
删除childVC.view.backgroundColor = JXRandomColor,功能正常
28.191 JXNavigationController:设置样式
28.192 0 <JXMessageCenterViewController: 0x78e718a0>
28.202 0 <JXDiscoverViewController: 0x78eba000>
28.204 0 <JXProfileViewController: 0x79e61970>
28.222 JXMessageCenterViewController: ButtonItem.enabled = NO
保留childVC.view.backgroundColor = JXRandomColor,disable颜色无效
58.016 JXNavigationController:设置样式
58.016 0 <JXMessageCenterViewController: 0x7a64b410>
58.031 JXMessageCenterViewController: ButtonItem.enabled = NO
58.032 0 <JXDiscoverViewController: 0x7a6a4f30>
58.035 0 <JXProfileViewController: 0x7a6abcc0>
//0 代表根控制器,后面代表哪个控制器被push 进来,这里关联的是Message 控制器
明显ButtonItem.enabled 的设置被推后了,可是到底是推后到哪里?既然已经设置了样式,为什么normal 有效,而disabled 无效?
相关代码:
JXTabBarViewController
- (void)viewDidLoad {
//添加自定义子控制器. 为了自定义push 进来的各种Controller 的左右上角的按钮
JXNavigationController *nav = [[JXNavigationController alloc] initWithRootViewController:childVC];
[self addChildViewController:nav];
NSLog(@"JXTabBarViewController: JXNavigationController-add");
childVC.view.backgroundColor = JXRandomColor;
}
JXNavigationController
+ (void)initialize
{
UIBarButtonItem *item = [UIBarButtonItem appearance];
// 设置普通按钮样式
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
[item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
// 设置不可用状态
NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
disableTextAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
disableTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
[item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
NSLog(@"JXNavigationController:设置样式");
}
- (void)viewDidLoad {
self.navigationItem.rightBarButtonItem.enabled = NO;
}
闭关修行中......