UINavigationController使用詳解_第1頁(yè)
UINavigationController使用詳解_第2頁(yè)
UINavigationController使用詳解_第3頁(yè)
UINavigationController使用詳解_第4頁(yè)
UINavigationController使用詳解_第5頁(yè)
已閱讀5頁(yè),還剩12頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、UINavigationController使用詳解1、UINavigationController使用流程UINavigationController為導(dǎo)航控制器,在iOS里經(jīng)常用到。我們看看它的如何使用:下面的圖顯示了導(dǎo)航控制器的流程。最左側(cè)是根視圖,當(dāng)用戶點(diǎn)擊其中的General項(xiàng)時(shí) ,General視圖會(huì)滑入屏幕;當(dāng)用戶繼續(xù)點(diǎn)擊Auto-Lock項(xiàng)時(shí),Auto-Lock視圖將滑入屏幕。相應(yīng)地,在對(duì)象管理上,導(dǎo)航控制器使用了導(dǎo)航堆棧。根視圖控制器在堆棧最底層,接下來(lái)入棧的是General視圖控制器和Auto-Lock視圖控制器。可以調(diào)用pushViewControllerAnimated

2、:方法將視圖控制器推入棧頂,也可以調(diào)用popViewControllerAnimated:方法將視圖控制器彈出堆棧。上圖來(lái)自蘋果官網(wǎng)。2、UINavigationController的結(jié)構(gòu)組成看下圖,UINavigationController有Navigation bar  ,Navigation View ,Navigation toobar等組成。3、新建一個(gè)項(xiàng)目,現(xiàn)在我們建立一個(gè)例子,看看如何使用UINavigationControllera.命名為NavigationControllerDemo,為了更好理解UINavigationController,我們選擇Empty

3、Application模板b、創(chuàng)建一個(gè)View Controller,命名為RootViewController,默認(rèn)勾上With XIB for user interface.c.選擇正確位置創(chuàng)建完成,這時(shí)項(xiàng)目里多了三個(gè)文件,分別是RootViewController.h RootViewController.m RootViewController.xib文件。d、將RootViewControoler添加到window上,在AppDelegate.m 文件的didFinishLaunchingWithOptions方法中創(chuàng)建添加navController,RootVi

4、ewController視圖。- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions    self.window = UIWindow alloc initWithFrame:UIScreen mainScreen bounds;    / Override point for customiza

5、tion after application launch.        RoowViewController *rootViewController = RoowViewController alloc init;    rootViewController.title = "rootView"        UINavig

6、ationController *nav = UINavigationController alloc initWithRootViewController:rootViewController;        self.window.rootViewController = nav;        rootViewController = nil;   &

7、#160;nav  = nil;        self.window.backgroundColor = UIColor whiteColor;    self.window makeKeyAndVisible;    return YES;e、 在RootViewControoler中添其它視圖,這里添加了一個(gè)tableView.- (void)viewDidLoad

8、60;   super viewDidLoad;        self.myArray = “navigationItem”,“Toolbar"        UITableView *tableView = UITableView alloc initWithFrame:self.view.bounds;  

9、60; tableView.backgroundColor = UIColor clearColor;    tableView.delegate = self;    tableView.dataSource = self;    self.view addSubview:tableView;    tableView = nil;#pragma

10、 mark - UITableViewDelegate And UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section    return self.myArray.count;- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)in

11、dexPath    static NSString *cellString_="tableCell"        UITableViewCell *cell=tableView dequeueReusableCellWithIdentifier:cellString_;    if (cell=nil)      

12、   cell=UITableViewCell alloc initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString_;        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;        cell.textLabel.

13、text=self.myArrayindexPath.row;        return cell;- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath        if (indexPath.row = 0)    

14、;     OneViewController *oneViewController = OneViewController alloc init;        self.navigationController pushViewController:oneViewController animated:YES;        oneV

15、iewController = nil;        f.現(xiàn)在Root視圖添加完成,看看運(yùn)行效果.現(xiàn)在點(diǎn)擊第一行就可以將視圖推到下一個(gè)視圖顯示了,控制視圖推進(jìn)推出的主要方法如下:pushViewController:viewController animated:BOOL(加載視圖控制器) 添加指定的視圖控制器并予以顯示popViewControllerAnimated:BOOL(推出當(dāng)前視圖控制器) 彈出并向左顯示前一個(gè)視圖popToViewController:viewController anim

16、ated:BOOL(推到指定視圖控制器) 回到指定視圖控制器, 也就是不只彈出一個(gè)popToRootViewControllerAnimated:BOOL退到根視圖控制器setNavigationBarHidden:BOOL animated:BOOL設(shè)置導(dǎo)航欄是否顯示 關(guān)于pushViewController:animated:的動(dòng)畫效果一般情況我們使用默認(rèn)的動(dòng)畫就行,但我們也可以自定動(dòng)畫內(nèi)容,我們可以使用CATransition來(lái)實(shí)現(xiàn)轉(zhuǎn)換動(dòng)畫效果,代碼如下:- (void)tableView:(UITableView *)tableView didSelectRowAt

17、IndexPath:(NSIndexPath *)indexPath        if (indexPath.row = 0)         OneViewController *oneViewController = OneViewController alloc init;       &#

18、160;        CATransition *animation = CATransition animation;        animation setDuration:0.5;        animation setType: kCATransitionMoveIn;  &

19、#160;     animation setSubtype: kCATransitionFromTop;        animation setTimingFunction:CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionDefault;          

20、60;     self.navigationController pushViewController:oneViewController animated:NO;        oneViewController = nil;                self.navi

21、gationController.view.layer addAnimation:animation forKey:nil;        4、navigationItem我們都知道navigationItem是UIViewController的一個(gè)屬性,這個(gè)屬性是為UINavigationController服務(wù)的。文檔中是這么解釋的“The navigation item used to represent the view controller in a parents navigatio

22、n bar. (read-only)”,即navigation item在navigation Bar代表一個(gè)viewController,具體一點(diǎn)兒來(lái)說(shuō)就是每一個(gè)加到navigationController的viewController都會(huì)有一個(gè)對(duì)應(yīng)的navigationItem,該對(duì)象由viewController以懶加載的方式創(chuàng)建,稍后我們可以在對(duì)象中堆navigationItem進(jìn)行配置,可以設(shè)置leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title以及prompt等屬性。前三個(gè)每一個(gè)都是一個(gè)UIBarB

23、uttonItem對(duì)象,最后兩個(gè)屬性是一個(gè)NSString類型描述,注意添加該描述以后NavigationBar的高度會(huì)增加30,總的高度會(huì)變成74(不管當(dāng)前方向是Portrait還是Landscape,此模式下navgationbar都使用高度44加上prompt30的方式進(jìn)行顯示)。當(dāng)然如果覺(jué)得只是設(shè)置文字的title不夠爽,你還可以通過(guò)titleview屬性指定一個(gè)定制的titleview,這樣你就可以隨心所欲了,當(dāng)然注意指定的titleview的frame大小,不要顯示出界。a、添加UIBarButtonItem,初始化UIBarButtonItem的方法有以下幾種方法:- (id)i

24、nitWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)t

25、arget action:(SEL)action;- (id)initWithCustomView:(UIView *)customView; /這個(gè)方法可以用來(lái)自定UIBarButtonItem這里重點(diǎn)介紹下- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;UIBarButtonSystemItem的風(fēng)格,這是系統(tǒng)自帶的按鈕風(fēng)格,看下圖,你不用一個(gè)個(gè)試驗(yàn),你也知道想用那個(gè)item,如下圖:給navigationIt

26、em 添加 UIBarButtonItem的方法有:property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;- (void)setLeftBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;- (void)setRightBarButtonItem:(UIBarButtonItem&

27、#160;*)item animated:(BOOL)animated;property(nonatomic,copy) NSArray *leftBarButtonItems NS_AVAILABLE_IOS(5_0);property(nonatomic,copy) NSArray *rightBarButtonItems NS_AVAILABLE_IOS(5_0);- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated NS

28、_AVAILABLE_IOS(5_0); - (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated NS_AVAILABLE_IOS(5_0);下面舉例說(shuō)明:- (void)viewDidLoad    super viewDidLoad;        self.view.backgroundColor = UIColor scr

29、ollViewTexturedBackgroundColor;    UIBarButtonItem *buttonItem1 = UIBarButtonItem alloc initWithTitle:"登錄"                         

30、;                                          style:UIBarButtonItemStyleBordered  &

31、#160;                                                 &

32、#160;             target:self                                  &#

33、160;                              action:selector(barButtonItemClick:);        UIBarButtonItem *buttonIte

34、m2 = UIBarButtonItem alloc initWithImage:UIImageimageNamed:"setUp.png"                                   

35、                                 style:UIBarButtonItemStyleBordered           &#

36、160;                                                 &#

37、160;     target:self                                          

38、60;                       action:selector(barButtonItemClick:);        UIBarButtonItem *buttonItem3 = UIBarButtonItem alloc initWi

39、thBarButtonSystemItem:UIBarButtonSystemItemAdd                                          &#

40、160;                                      target:self         

41、60;                                                 

42、60;                    action:selector(barButtonItemClick:);        UIButton *button = UIButton alloc initWithFrame:CGRectMake(0, 7, 50

43、, 30);    button.backgroundColor = UIColor blueColor;    button setTitle:"按扭" forState:UIControlStateNormal;    UIBarButtonItem *buttonItem4 = UIBarButtonItem alloc initWithCustomView:button

44、;        self.navigationItem.leftBarButtonItem = buttonItem1;    self.navigationItem.rightBarButtonItems = buttonItem2, buttonItem3, buttonItem4;/UIBarButtonItem擊事件- (void)barButtonItemClick:(UIBarButtonItem *)barButton&

45、#160;   /添加你要響應(yīng)的代碼運(yùn)行效果.b、自定義backBarButtonItem在項(xiàng)目中使用UINavigationController導(dǎo)航,當(dāng)?shù)谝患?jí)頁(yè)面的title較長(zhǎng),在進(jìn)入第二級(jí)頁(yè)面后返回按鈕backBarButtonItem的title就會(huì)變得很長(zhǎng),于是使用代碼對(duì)leftButtonItem的title文本進(jìn)行修改,無(wú)論是設(shè)置self.navigationItem.leftBarButtonItem.title = "返回"還是self.navigationItem.backBarButtonItem.title = &quo

46、t;返回"都沒(méi)有效果,title始終不會(huì)發(fā)生變化,要怎第才能修改返回按扭的title呢?原來(lái)使用pushViewController切換到下一個(gè)視圖時(shí),navigationController按照以下3條順序更改導(dǎo)航欄的左側(cè)按鈕。1)、如果B視圖有一個(gè)自定義的左側(cè)按鈕(leftBarButtonItem),則會(huì)顯示這個(gè)自定義按鈕;2)、如果B沒(méi)有自定義按鈕,但是A視圖的backBarButtonItem屬性有自定義項(xiàng),則顯示這個(gè)自定義項(xiàng);3)、如果前2條都沒(méi)有,則默認(rèn)顯示一個(gè)后退按鈕,后退按鈕的標(biāo)題是A視圖的標(biāo)題。按照這個(gè)解釋,我把UIBarButtonItem *backItem這

47、段代碼放在A視圖的pushViewController語(yǔ)句之前。- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath        if (indexPath.row = 0)         ItemOneViewController *itemOne = ItemOneViewCont

48、roller alloc init;        itemOne.title = "addItem"        self.navigationController pushViewController:itemOne animated:YES;        itemOne = nil;    

49、;    else        ItemTwoViewController *itemTwo = ItemTwoViewController alloc init;        itemTwo.title = "setBackItemTItle"      

50、0;         UIBarButtonItem *back = UIBarButtonItem alloc initWithTitle:"返回"                          

51、;                                       style:UIBarButtonItemStyleBordered     &

52、#160;                                                 &

53、#160;        target:nil                                       

54、60;                        action:nil;        self.navigationItem.backBarButtonItem = back;       &#

55、160;back = nil;                self.navigationController pushViewController:itemTwo animated:YES;        itemTwo = nil;       

56、; OK問(wèn)題解決了,B視圖的后退按鈕的標(biāo)題變成“返回“了,也可以使用自定義的返回按扭。c、自定義titleView,UINavigationController的title可以用別的view替代,比如用UIview、 UIButton、 segmentedController、UILable等,在下面的代碼我用UIButton來(lái)演示.代碼如下- (void)viewDidLoad    super viewDidLoad;        UIButton&

57、#160;*button = UIButton alloc initWithFrame:CGRectMake(0, 0, 90, 35);    button.backgroundColor = UIColor blueColor;    button setTitle:"自定義titleView" forState:UIControlStateNormal;    

58、self.navigationItem.titleView = button;注意:view的大小不要大小導(dǎo)航欄的大小,要不在就會(huì)顯示不下。運(yùn)行效果如圖:5、Toolbar 在前面我們講了UINavigationController有Navigation bar  ,Navigation View ,Navigation toolbar等組成,下面就講解一下toolbar的使用,toolbar在UINavigationController默認(rèn)是隱藏的,可通過(guò)下面的代碼顯示出來(lái)。self.navigationController.toolbarHidden 

59、;= NO;或self.navigationController setToolbarHidden:NO animated:YES;在ToolBar上添加UIBarButtonItem,新建幾個(gè)UIBarButtonItem,然后以數(shù)組的形式添加到Toolbar中    super viewDidLoad;        self.navigationController.toolbarHidden = NO;

60、60;       UIBarButtonItem*one=UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemAdd                          &

61、#160;                                              target:nil 

62、60;                                                 

63、60;                     action:nil;    UIBarButtonItem*two=UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks     &#

64、160;                                                 &#

65、160;                 target:nil                              

66、0;                                          action:nil;    UIBarButton

67、Item*three=UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemAction                                  

68、0;                                    target:nil            

69、                                                  

70、         action:nil;    UIBarButtonItem*four=UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemEdit                  

71、60;                                                 

72、60; target:nil                                               

73、;                       action:nil;    UIBarButtonItem*flexItem=UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  &

74、#160;                                                 &

75、#160;                     target:nil                          

76、60;                                               action:nil;

77、0;   self setToolbarItems:flexItem,one,flexItem,two,flexItem,three,flexItem,four,flexItem;注意:用self.navigationController.toolbar setItems:(NSArray *) animated:(BOOL)這個(gè)方法添加item是不起效果的。運(yùn)行效果:但調(diào)用這個(gè)方法后所有UINavigationController中的viewController都會(huì)顯示出toolbar.如下圖:所以為了不出現(xiàn)這種情況可以不用UINavigationCo

78、ntroller中的toolbar、需重寫toolbar來(lái)實(shí)現(xiàn)。這樣在返回上級(jí)視圖時(shí)就不會(huì)出現(xiàn)本來(lái)隱藏著的toolbar,代碼如下:- (void)viewDidLoad    super viewDidLoad;        UIBarButtonItem*one=UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemAdd   

79、0;                                                 

80、0;               target:nil                                 

81、                                    action:nil;    UIBarButtonItem*two=UIBarButtonItem allocini

82、tWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks                                         

83、;                            target:nil                    &

84、#160;                                                action:

85、nil;    UIBarButtonItem*three=UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemAction                            &

86、#160;                                          target:nil     

87、60;                                                 

88、60;               action:nil;    UIBarButtonItem*four=UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemEdit            

89、                                                  

90、        target:nil                                        &#

91、160;                             action:nil;    UIBarButtonItem*flexItem=UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarBut

92、tonSystemItemFlexibleSpace                                             

93、0;                            target:nil                    

94、                                                  

95、    action:nil;        UIToolbar *toolBar = UIToolbar alloc initWithFrame:CGRectMake(0,self.view.frame.size.height - 44, self.view.frame.size.width, 44);    toolBar setBarStyle:UIB

96、arStyleBlack;    toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;    toolBar setItems:flexItem,one,flexItem,two,flexItem,three,flexItem,four,flexItemanimated:YES;    self.view addSubview:toolBar;運(yùn)行效果圖6、設(shè)置導(dǎo)

97、航欄的一些屬性設(shè)置導(dǎo)航欄中title的字體效果    NSDictionary *dict = UITextAttributeTextColor: UIColor colorWithRed:27.0/255green:125.0/255 blue:197.0/255 alpha:1,                   

98、;    UITextAttributeTextShadowColor: UIColor whiteColor;    self.navigationController.navigationBar.titleTextAttributes = dict;運(yùn)行效果圖:設(shè)置導(dǎo)航欄的背影色    self.navigationController.navigationBar.tintColor = UIColor redColor;上面方法只

99、能用在ios7以下有效,在ios7以下設(shè)置的是導(dǎo)航中按扭字體的顏色,所以在ios7中修改導(dǎo)航欄的背影色要用    self.navigationController.navigationBar.barTintColor = UIColor blueColor;可能過(guò)系統(tǒng)的版本來(lái)進(jìn)行相應(yīng)的設(shè)置,如果不加判斷而直接寫上兩個(gè)屬性的話低版本的統(tǒng)中就會(huì)閃退,因?yàn)?.0以下的紡統(tǒng)沒(méi)有barTintColor這個(gè)屬性值    if (UIDevice currentDevice 

100、systemVersion floatValue >= 7.0)         / iOS 7 code        self.navigationController.navigationBar.barTintColor = UIColor brownColor;        else 

101、0;       / iOS 6.x code        self.navigationController.navigationBar.tintColor = UIColor brownColor;    動(dòng)行效果圖:用圖片設(shè)置導(dǎo)航欄的背影    self.navigationController.navigationBar set

102、BackgroundImage:UIImage imageNamed:"nav_bg.png"forBarMetrics:UIBarMetricsDefault;動(dòng)行效果圖:將導(dǎo)航欄設(shè)置為半透明,        self.navigationController.navigationBar.translucent = YES; /設(shè)置這個(gè)屬性后視圖的坐標(biāo)會(huì)上移到導(dǎo)航欄的上面,將導(dǎo)航欄設(shè)置為透明的效果先做一張全透明的圖片1*1的像素就行,作為UINaviga

103、tionBar的背景色,然后將barStyle設(shè)置成通道就可以了。    self.navigationController.navigationBar setBackgroundImage:UIImage imageNamed:"clear.png"forBarMetrics:UIBarMetricsDefault;    self.navigationController.navigationBar.barStyle = UIBarStyleBlack

104、Translucent;動(dòng)行效果圖: 方法有很多,個(gè)人覺(jué)得這個(gè)最簡(jiǎn)單,可以現(xiàn)實(shí)各種效果??偨Y(jié)下上面講到的屬性使用方法    /設(shè)置導(dǎo)航欄的背影色,ios6和ios7要區(qū)別對(duì)待,設(shè)置為透明色(clearColor)時(shí)無(wú)效,為黑色背影    if (UIDevice currentDevice systemVersion floatValue >= 7.0)         / iOS 7 code        self.navigationController.navigationBar.barTintColor = UIColor clearColor;        else   

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論