UINavigationController 、UINavigationBar 、UINavigationItem 超清晰直觀詳解


UINavigationController 部分

1、 UINavigationController 是一個容器類。里面盛放的是UIViewController。

容器的意思是,如果你不放入UIViewController,里面就是空的,什么也沒有。

這個容器在管理UIViewController時,遵循棧管理的原則(后進先出)。

 UIViewController的入棧操作

UIViewController *aViewController = [[UIView alloc] init;
[self.navigationController pushViewController:aViewController   animated:NO];

出棧操作

[self.navigationController popViewControllerAnimated:YES]; 

2、UINavigationController雖然是個容器,但也不是干巴巴的空殼。它由以下四部分組成:Navigation toolbar、Custom content、Navigation bar、,Navigation view。其中,我猜測,入棧的UIViewController 其界面應該就展示在Custom content區域。

參考:http://hi.baidu.com/iphone8/item/f3b3cb6f00cded2e68105b21

UINavigationItem部分

另外據悉,UINavigationController會為每一個入棧的UIViewController生成一個UINavigationItem. UIViewController通過修改UINavigationItem可以控制UINavigationBar上的按鈕和標題等。如下:

你可以通過設置self.navigationItem.leftBarButtonItem為某個ButtonItem,

self.navigationItem.leftBarButtonItem

self.navigationItem.rightBarButtonItem

self.navigationItem.backBarButtonItem

self.navigationItem.titleView等等

注:1、這里的self 指的是UIViewController。

2、如果你在新視圖中不修改backBarButtonItem 或leftBarButtonItem UINavigationController 會自動添加左邊返回按鈕用以返回了一個視圖。總體的顯示原則如下:

 1)、Left side of the navigationBar 左按鈕

  a)如果當前的viewController設置了leftBarButtonItem,則顯示當前VC所自帶的leftBarButtonItem。

  b)如果當前的viewController沒有設置leftBarButtonItem,且當前VC不是rootVC的時候,則顯示前一層VC的backBarButtonItem。如果前一層的VC沒有顯示的指定backBarButtonItem的話,系統將會根據前一層VC的title屬性自動生成一個back按鈕,並顯示出來。

  c)如果當前的viewController沒有設置leftBarButtonItem,且當前VC已是rootVC的時候,左邊將不顯示任何東西。

  此處注意:5.0中新增加了一個屬性leftItemsSupplementBackButton,通過指定該屬性為YES,可以讓leftBarButtonItem和backBarButtonItem同時顯示,其中leftBarButtonItem顯示在backBarButtonItem的右邊。

  2)、title 標題

  a)如果當前VC通過 .navigationItem.titleView指定了自定義的titleView,系統將會顯示指定的titleView,此處要注意自定義titleView的高度不要超過navigationBar的高度,否則會顯示出界。

  b)如果當前VC沒有指定titleView,系統則會根據當前VC的title或者當前VC的navigationItem.title的內容創建一個UILabel並顯示,其中如果指定了navigationItem.title的話,則優先顯示navigationItem.title的內容。

  3)、Right side of the navigationBar  右按鈕

  a)如果當前VC指定了rightBarButtonItem的話,則顯示指定的內容。

  b)如果當前VC沒有指定rightBarButtonItem的話,則不顯示任何東西。

 

參考:http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html

UINavigationBar 部分

切記,UINavigationBar只有一個,它永遠只屬於UINavigationController,是所有UIViewController公用的。所以,當你修改了UINavigationBar的背景圖片或者顏色時,相當於修改了所有UIViewController的NavigationBar的背景圖片或顏色。

下面,通過如下視圖,我們來了解以下,當系統展示某個具體的UIViewController時,整個頁面所有元素的從屬關系。

參考:http://www.cnblogs.com/iOS-dd/archive/2013/06/12/3132366.html

uinavigationController、uinavigationBar、uinavigationBarItem三者的區別,現在明白了嗎?

通俗地說就是,uinavigationController是個容器,里面可以裝很多uiviewController。裝這么多uiviewController讓用戶怎么控制它們呢,總得有個工具吧。這個工具就是uinavigationBar。一個容器就這么一個bar,相當於控制台吧。但是,管理那么多uiviewController,控制台上得按鈕啊、標題啊,都千篇一律是不是看起來太無聊了。為了解決這個問題,uinavigationController為每個uiviewController生成一個uinavigationBarItem,通過這個uinavigationBarItem可以改變控制台“上面”得按鈕和標題。如果你不自定義uinavigationBarItem,uinavigationController會使用默認的。

  • l    

UINavigationController是UIViewController的子類,UINavigationBar是UIView的子類。

UINavigationBar是UINavigationController的一個組成部分,就是上面的那個導航欄。

UINavigationBar又有UINavigationItem組成。

UINavigationItem則有title,按鈕,提示文本等組成,就是我們看到的title文字,右上角的按鈕。 

  • l   navigation item在navigation Bar代表一個viewController,具體一點兒來說就是每一個加到navigationController的viewController都會有一個對應的navigationItem
  • l   一個導航控制器控制多個視圖,NavigationBar上的leftItem,rightItem,title是由當前的視圖控制器控制的

 

所有參考:

UINavigationController使用詳解  寫的不錯  http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html

UINavigationController使用的一些技巧  講的不錯

UINavigationController,UINavigationBar  全面但 凌亂

UINavigationController    講的比較直白

IOS開發筆記_3.UINavigationController層次關系   絕對牛b清晰

 

實踐經驗

1、self.navigationItem.leftBarButtonItem如何自定義位置:右移

方案:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ITEM_WIDTH, ITEM_HEIGHT)];
[btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:@"naviationbar_button_normal.png" ] forState:UIControlStateNormal];//不要用setbackgroudimage
[btn setImage:[UIImage imageNamed:@"naviationbar_button_pressed.png" ] forState:UIControlStateHighlighted];
btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0); //方案
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = backItem;

來源:http://bbs.csdn.net/topics/390609070

2、隱藏 TabBar 的方法1(推薦)

如在A_VC中,push B_VC, 則在B_VC中寫如下代碼

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;        //隱藏TabBar
    [self.navigationItem setHidesBackButton:YES];     //隱藏NavigationController自動生成的返回按鈕
    self.navigationController.navigationBar.tintColor = [UIColor clearColor]; //返回按鈕顏色

}

隱藏 TabBar 的方法2

在A_VC中中寫如下代碼

-(IBAction)btnOnClicked:(id)sender
{
    A_VC *a_VC = [[[A_VC alloc]init]autorelease];
    self.hidesBottomBarWhenPushed = YES;    //用於隱藏tabBar
    [self.navigationController pushViewController:a_VC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM