自定義導(dǎo)航欄按鈕通過(guò)navigationItem.leftBarButtonItem,可能大家都知道,但關(guān)鍵點(diǎn)在于哪個(gè)控制器的navigationItem,舉下面幾種情況
一,UINavigationController+UIViewController,這種方式是常規(guī)的,沒(méi)有問(wèn)題
AppDelegate.m
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; self.window.rootViewController = nav;
ViewController.m
UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(10, 7, 83, 30);
leftBtn.backgroundColor = [UIColor orangeColor];
UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = leftItem;
二,UINavigationController+UITabBarController+UIViewController,這種方式設(shè)置了沒(méi)有效果
AppDelegate.m
UITabBarController *tabbar = [[UITabBarController alloc] init];
tabbar.view.backgroundColor = [UIColor greenColor];
ViewController *vc = [[ViewController alloc] init];
[tabbar addChildViewController:vc];
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor yellowColor];
vc1.title = @"民宿";
[tabbar addChildViewController:vc1];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tabbar];
nav.view.backgroundColor = [UIColor redColor];
self.window.rootViewController = nav;
ViewController.m
UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(10, 7, 83, 30);
leftBtn.backgroundColor = [UIColor orangeColor];
UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = leftItem;
三,UINavigationController +UIViewController +UIViewController 這樣設(shè)置也沒(méi)有效果
AppDelegate.m
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
ViewController.m
ViewControllerB *viewB = [[ViewControllerB alloc] init];
[self addChildViewController:viewB];
ViewControllerB.m
// 自定義導(dǎo)航欄左側(cè)按鈕
UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(10, 7, 83, 30);
leftBtn.backgroundColor = [UIColor orangeColor];
UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = leftItem;
self.view.backgroundColor = [UIColor purpleColor];
我就在考慮為什么有的可以有的不可以,每個(gè)UIViewController都有navigationItem,難道navigationItem不是都指向統(tǒng)一的navigationController.navigationBar的navigationItem,
查看官方文檔
This is a unique instance of UINavigationItem created to represent the view controller when it is pushed onto a navigation controller. The first time the property is accessed, the UINavigationItem object is created. Therefore, you should not access this property if you are not using a navigation controller to display the view controller. To ensure the navigation item is configured, you can either override this property and add code to create the bar button items when first accessed or create the items in your view controller'??s initialization code.
我分別打印了UINavigationController,UITabBarController,UIViewController的navigationItem內(nèi)存地址,結(jié)果不一樣,和官方文檔的說(shuō)明一致,每個(gè)控制器都會(huì)新建一個(gè)navigationItem,至于把哪個(gè)navigationItem加到要顯示的navigationBar上,主要是由[[UINavigationController alloc] initWithRootViewController:vc];導(dǎo)航控制器的根試圖控制器決定的。
我打印出了,
self.navigationController.navigationBar.items.lastObject.leftBarButtonItems
結(jié)果和initWithRootViewController試圖控制器里的navigationItem上的leftBarButtonItem的內(nèi)存地址一致。(以自定義導(dǎo)航欄做按鈕為示例,推理),navigationItem是只讀的,我猜想是以navigationItem為媒介來(lái)操作leftBarButtonItem,至于蘋果為什么這樣設(shè)計(jì),不是很明白,猜想會(huì)不會(huì)為了高內(nèi)聚低耦合。。??(補(bǔ)充一點(diǎn),如果在控制器里直接設(shè)置
self.navigationController.navigationBar.items.lastObject.leftBarButtonItems = @[leftItem,leftItem1];
是可以的。。。。)
在尋找答案的過(guò)程中,也用了reveal工具來(lái)幫助我分析,在剛開始的分析中,我會(huì)問(wèn)自己我怎么看不到控制器,怎么都是試圖,慢慢的我領(lǐng)悟到,其實(shí)整個(gè)APP就是圖層的疊加顯示,是動(dòng)態(tài)的圖層,因?yàn)橛芯W(wǎng)絡(luò)和交互。而控制器只不過(guò)是人為的抽取出來(lái)來(lái)出來(lái)一個(gè)view上的所有試圖的交互和邏輯。一點(diǎn)小心得記錄一下,雖然可能對(duì)于別人而言早都理解了,但是對(duì)于我卻是不小的欣喜。是從知道了到理解的質(zhì)變。今天是臘八,口號(hào),臘八臘八發(fā)發(fā)發(fā),????!
