做微博項(xiàng)目時(shí)候遇到的
首先上圖辛臊,看看需求是什么樣的
點(diǎn)擊下方的橘黃色?按鈕纹份,即modal出一個(gè)控制器典格,即發(fā)微博的界面控制器
要求navigationbar上面的左右兩個(gè)barbuttonitem字體顏色是橘黃色祖娘,但是在編輯界面沒(méi)有文字的時(shí)候丽焊,右上角的發(fā)送按鈕不可用,且顏色為灰色
下面是我的做法
由于發(fā)微博界面控制器被一個(gè)導(dǎo)航控制器包裝箕肃,所以選擇在自定義的導(dǎo)航控制器里統(tǒng)一設(shè)置UIBarButtonItem的顏色婚脱,下面是設(shè)置顏色的代碼部分
YLNavigationController.h
#import "YLNavigationController.h"
@implementation YLNavigationController
+(void)initialize
{
//設(shè)置整個(gè)項(xiàng)目的item狀態(tài)
UIBarButtonItem *item = [UIBarButtonItem appearance];
//設(shè)置item普通狀態(tài)
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
attrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
//設(shè)置item不可用狀態(tài)
NSMutableDictionary *disabledAttrs = [NSMutableDictionary dictionary];
disabledAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
disabledAttrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
[item setTitleTextAttributes:disabledAttrs forState:UIControlStateDisabled];
}
在首頁(yè)部分,底部的tabbar也是自定義勺像,點(diǎn)擊按鈕之后障贸,由tabbar的 代理YLTabBarController來(lái)modal發(fā)微博控制器,下面是YLTabBarController中關(guān)于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中設(shè)置左右兩個(gè)barbuttonitem吟宦,關(guān)于這部分代碼展示如下
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;
}
那么這個(gè)時(shí)候問(wèn)題就來(lái)了篮洁,顯示的時(shí)候右上角的barbuttonitem的顏色一直為橘黃色,按鈕也確實(shí)點(diǎn)擊之后沒(méi)有反應(yīng)殃姓,一般出現(xiàn)這個(gè)問(wèn)題袁波,就是YLComposeViewController
的view提前加載,但是我們能看到蜗侈,設(shè)置顏色是在+(void)initialize
中進(jìn)行的篷牌,這個(gè)方法是在調(diào)用對(duì)象第一個(gè)方法之前就會(huì)調(diào)用的,而我所有有關(guān)YLComposeViewController
的代碼全部展示出來(lái)了踏幻,當(dāng)YLNavigationController
的+(void)initialize
方法調(diào)用時(shí)枷颊,YLComposeViewController
壓根毛都沒(méi)有,并且我也分別在+(void)initialize
方法和YLComposeViewController
的viewDidLoad
方法下斷點(diǎn)该面,結(jié)果表明+(void)initialize
方法確實(shí)先加載夭苗。沒(méi)辦法我只能嘗試將self.navigationItem.rightBarButtonItem.enabled = NO;
這段方法往后放,放在了viewWillAppear
方法中隔缀,代碼如下
YLComposeViewController.m
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationItem.rightBarButtonItem.enabled = NO;
}
結(jié)果居然可行L庠臁!猾瘸!右邊發(fā)送按鈕不可點(diǎn)界赔,且顏色為灰色,這個(gè)時(shí)候我就很困惑了牵触,我甚至也有些懷疑是不是代碼結(jié)構(gòu)太復(fù)雜中間有什么方法我漏掉了淮悼,于是我決定把這兩個(gè)控制器抽出來(lái),單獨(dú)寫一個(gè)小demo荒吏,測(cè)試一下,代碼不多渊鞋,我就羅列在下面绰更,由于.h文件中很干凈瞧挤,故只羅列.m文件。
YLNavigationController.m
+(void)initialize
{
//設(shè)置整個(gè)項(xiàng)目的item狀態(tài)
UIBarButtonItem *item = [UIBarButtonItem appearance];
//設(shè)置item普通狀態(tài)
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
attrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
[item setTitleTextAttributes:attrs forState:UIControlStateNormal];
//設(shè)置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中并沒(méi)添加代碼儡湾,就不羅列了特恬,基本就是將上面展示的代碼復(fù)制一遍,這也算是干了這么多年的機(jī)械養(yǎng)成的習(xí)慣徐钠,控制變量來(lái)排查問(wèn)題癌刽,運(yùn)行之后發(fā)現(xiàn)沒(méi)有效果,第一次寫博客不知道怎么錄屏尝丐,只能截圖了显拜。
當(dāng)然,放到viewWillAppear
里面是好使得爹袁。
這下問(wèn)題嚴(yán)重了远荠,可能就牽扯到蘋果內(nèi)部封裝的一些代碼問(wèn)題了,這個(gè)我也確實(shí)不清楚失息,那以后遇到這種情況就直接放到viewWillAppear
里面唄譬淳,我開始也是這么想的,不過(guò)事實(shí)證明還是too young啊盹兢,上面是將控制器modal出來(lái)邻梆,而我決定試一下另一種情況,就是將控制器push出來(lái)绎秒,看看效果浦妄,稍微改改代碼就好。
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;
}