zoukankan      html  css  js  c++  java
  • iOS开发解决:iOS8.1中UIBarButtonItem的setTitleTextAttributes对Disabled颜色设置无效问题

    (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中测试是正常的。

  • 相关阅读:
    零零碎碎
    MFC入门--显示静态图片及调用本地软件
    Python版本OpenCV安装配置及简单实例
    用星星画菱形--Java
    pycharm IDE在导入自定义模块时提示有错,但实际没错
    Cmd使用方式--命令行运行程序
    cv2 & PIL(pillow)显示图像
    C++命令行多文件编译(g++)
    MNIST多图显示--Python练习
    visual studio 2017--括号自动补全
  • 原文地址:https://www.cnblogs.com/tate-zwt/p/4506388.html
Copyright © 2011-2022 走看看