做微博项目时候遇到的
首先上图,看看需求是什么样的
微博首页发微博界面点击下方的橘黄色➕按钮,即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
方法和YLComposeViewController
的viewDidLoad
方法下断点,结果表明+(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;
}
网友评论