關于統(tǒng)一設置UIBarButtonItem顏色的問題

做微博項目時候遇到的

首先上圖,看看需求是什么樣的

微博首頁

點擊下方的橘黃色?按鈕,即modal出一個控制器,即發(fā)微博的界面控制器

發(fā)微博界面

要求navigationbar上面的左右兩個barbuttonitem字體顏色是橘黃色,但是在編輯界面沒有文字的時候,右上角的發(fā)送按鈕不可用,且顏色為灰色

下面是我的做法

由于發(fā)微博界面控制器被一個導航控制器包裝,所以選擇在自定義的導航控制器里統(tǒng)一設置UIBarButtonItem的顏色,下面是設置顏色的代碼部分

YLNavigationController.h
#import "YLNavigationController.h"

@implementation YLNavigationController

+(void)initialize
{
  //設置整個項目的item狀態(tài)
  UIBarButtonItem *item = [UIBarButtonItem appearance];

  //設置item普通狀態(tài)
  NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
  attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
  attrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
  [item setTitleTextAttributes:attrs forState:UIControlStateNormal];

  //設置item不可用狀態(tài)
  NSMutableDictionary *disabledAttrs = [NSMutableDictionary dictionary];
  disabledAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
  disabledAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
  [item setTitleTextAttributes:disabledAttrs forState:UIControlStateDisabled];
}

在首頁部分,底部的tabbar也是自定義,點擊按鈕之后,由tabbar的 代理YLTabBarController來modal發(fā)微博控制器,下面是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];
}

下面則是在發(fā)微博控制器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:@"發(fā)送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}
那么這個時候問題就來了,顯示的時候右上角的barbuttonitem的顏色一直為橘黃色,按鈕也確實點擊之后沒有反應,一般出現(xiàn)這個問題,就是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;
}
結果居然可行?。?!右邊發(fā)送按鈕不可點,且顏色為灰色,這個時候我就很困惑了,我甚至也有些懷疑是不是代碼結構太復雜中間有什么方法我漏掉了,于是我決定把這兩個控制器抽出來,單獨寫一個小demo,測試一下,代碼不多,我就羅列在下面,由于.h文件中很干凈,故只羅列.m文件。
YLNavigationController.m
+(void)initialize
{
  //設置整個項目的item狀態(tài)
  UIBarButtonItem *item = [UIBarButtonItem appearance];

  //設置item普通狀態(tài)
  NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
  attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
  attrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
  [item setTitleTextAttributes:attrs forState:UIControlStateNormal];

  //設置item不可用狀態(tài)
  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:@"發(fā)送" 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中并沒添加代碼,就不羅列了,基本就是將上面展示的代碼復制一遍,這也算是干了這么多年的機械養(yǎng)成的習慣,控制變量來排查問題,運行之后發(fā)現(xiàn)沒有效果,第一次寫博客不知道怎么錄屏,只能截圖了。
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:@"發(fā)送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}
運行的結果是這樣的
demo截屏

好吧,估計像我這樣費盡口舌講了半天卻沒有告訴具體原因是什么的人,真的是不多了,因為我一直是在自學,所以總覺得是我自己出錯,這個問題想了很久也想不明白,問別人又幾句話說不清,所以決定先寫出來,主要是記下我做微博項目的過程,以及解決問題的一些思路,希望能有精通此道的大神看到了能給些指點。測試代碼在github上。點擊https://github.com/shidayangli/problemsWithBarButtonItem.git

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容