ios - 关于UIBarButtonItem 的UIControlStateDisabled 颜色设置无效的原因
阿神
阿神 2017-04-17 17:38:06
[iOS讨论组]

代码的执行顺序如上图所示,去掉

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;
}
阿神
阿神

闭关修行中......

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号