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

  • 相关阅读:
    彻底解决Spring MVC 中文乱码 问题
    侯捷 c++面向对象程序设计
    folly学习心得(转)
    vcpkg —— VC++ 打包工具
    Windows下安装GCC
    Linux下编写 makefile 详细教程
    侯捷stl学习笔记链接
    《Effective C++(第三版)》-笔记
    CentOS 7 安装Boost 1.61
    Windbg查看w3wp进程占用的内存及.NET内存泄露,死锁分析
  • 原文地址:https://www.cnblogs.com/tate-zwt/p/4506388.html
Copyright © 2011-2022 走看看