美文网首页iOS开发
关于统一设置UIBarButtonItem颜色的问题

关于统一设置UIBarButtonItem颜色的问题

作者: _table | 来源:发表于2015-10-16 12:37 被阅读8998次

做微博项目时候遇到的

首先上图,看看需求是什么样的

微博首页

点击下方的橘黄色➕按钮,即modal出一个控制器,即发微博的界面控制器

发微博界面

要求navigationbar上面的左右两个barbuttonitem字体颜色是橘黄色,但是在编辑界面没有文字的时候,右上角的发送按钮不可用,且颜色为灰色

下面是我的做法

由于发微博界面控制器被一个导航控制器包装,所以选择在自定义的导航控制器里统一设置UIBarButtonItem的颜色,下面是设置颜色的代码部分

YLNavigationController.h
#import "YLNavigationController.h"

@implementation YLNavigationController

+(void)initialize
{
  //设置整个项目的item状态
  UIBarButtonItem *item = [UIBarButtonItem appearance];

  //设置item普通状态
  NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
  attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
  attrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
  [item setTitleTextAttributes:attrs forState:UIControlStateNormal];

  //设置item不可用状态
  NSMutableDictionary *disabledAttrs = [NSMutableDictionary dictionary];
  disabledAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
  disabledAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
  [item setTitleTextAttributes:disabledAttrs forState:UIControlStateDisabled];
}

在首页部分,底部的tabbar也是自定义,点击按钮之后,由tabbar的 代理YLTabBarController来modal发微博控制器,下面是YLTabBarController中关于modal的一部分代码

YLTabBarController.m
#pragma mark - YLTabBarDelegate method
-(void)tabBarDidClickButton:(YLTabBar *)tabBar
{
  YLComposeViewController *composeViewController = [[YLComposeViewController alloc] init];
  YLNavigationController *navi = [[YLNavigationController alloc] initWithRootViewController:composeViewController];
  [self presentViewController:navi animated:YES completion:nil];
}

下面则是在发微博控制器YLComposeViewController中设置左右两个barbuttonitem,关于这部分代码展示如下

YLComposeViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}
那么这个时候问题就来了,显示的时候右上角的barbuttonitem的颜色一直为橘黄色,按钮也确实点击之后没有反应,一般出现这个问题,就是YLComposeViewController的view提前加载,但是我们能看到,设置颜色是在+(void)initialize中进行的,这个方法是在调用对象第一个方法之前就会调用的,而我所有有关YLComposeViewController的代码全部展示出来了,当YLNavigationController+(void)initialize方法调用时,YLComposeViewController压根毛都没有,并且我也分别在+(void)initialize方法和YLComposeViewControllerviewDidLoad方法下断点,结果表明+(void)initialize方法确实先加载。没办法我只能尝试将self.navigationItem.rightBarButtonItem.enabled = NO;这段方法往后放,放在了viewWillAppear方法中,代码如下
YLComposeViewController.m
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}
结果居然可行!!!右边发送按钮不可点,且颜色为灰色,这个时候我就很困惑了,我甚至也有些怀疑是不是代码结构太复杂中间有什么方法我漏掉了,于是我决定把这两个控制器抽出来,单独写一个小demo,测试一下,代码不多,我就罗列在下面,由于.h文件中很干净,故只罗列.m文件。
YLNavigationController.m
+(void)initialize
{
  //设置整个项目的item状态
  UIBarButtonItem *item = [UIBarButtonItem appearance];

  //设置item普通状态
  NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
  attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
  attrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
  [item setTitleTextAttributes:attrs forState:UIControlStateNormal];

  //设置item不可用状态
  NSMutableDictionary *disabledAttrs = [NSMutableDictionary dictionary];
  disabledAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
  disabledAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
  [item setTitleTextAttributes:disabledAttrs forState:UIControlStateDisabled];
}
YLComposeViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}
ViewController.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    YLComposeViewController *com = [[YLComposeViewController alloc] init];
    YLNavigationController *nav = [[YLNavigationController alloc] initWithRootViewController:com];
    [self presentViewController:nav animated:YES completion:nil];
}
AppDelegate中并没添加代码,就不罗列了,基本就是将上面展示的代码复制一遍,这也算是干了这么多年的机械养成的习惯,控制变量来排查问题,运行之后发现没有效果,第一次写博客不知道怎么录屏,只能截图了。
demo截屏
当然,放到viewWillAppear里面是好使得。
这下问题严重了,可能就牵扯到苹果内部封装的一些代码问题了,这个我也确实不清楚,那以后遇到这种情况就直接放到viewWillAppear里面呗,我开始也是这么想的,不过事实证明还是too young啊,上面是将控制器modal出来,而我决定试一下另一种情况,就是将控制器push出来,看看效果,稍微改改代码就好。
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    ViewController *vc = [[ViewController alloc] init];
    YLNavigationController *navi = [[YLNavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = navi;
    return YES;
}
ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}
运行的结果是这样的
demo截屏

好吧,估计像我这样费尽口舌讲了半天却没有告诉具体原因是什么的人,真的是不多了,因为我一直是在自学,所以总觉得是我自己出错,这个问题想了很久也想不明白,问别人又几句话说不清,所以决定先写出来,主要是记下我做微博项目的过程,以及解决问题的一些思路,希望能有精通此道的大神看到了能给些指点。测试代码在github上。点击https://github.com/shidayangli/problemsWithBarButtonItem.git

相关文章

网友评论

  • 表没食子儿茶素没食子酸酯EGC:因为viewWillAppear会在每次你看到视图的时候重新调用一次,所以如果有要更新的部分要写在这个方法里
  • b1261b97064c:关键是问题怎么解决啊,都不知道为什么,源代码里面这个问题倒是没有出现
  • 贝戋丶马户:为了给你点赞,特意注册了一个简书账号,虽然最后没有说明问题的本质出在哪里,但是用你的方法的确解决了我的问题,而且你分析问题的方式也很棒 :kissing_heart:
  • 哈么么茶:一样的问题 楼主解决了吗? :disappointed_relieved:

本文标题:关于统一设置UIBarButtonItem颜色的问题

本文链接:https://www.haomeiwen.com/subject/tgtacttx.html