UINavigationController的簡(jiǎn)單介紹

UINavigationController是IOS編程中比較常用的一種容器view controller,也就是導(dǎo)航欄控制器。

    
(這里是文檔里的圖片)

1.創(chuàng)建一個(gè)UINavigationController

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];   
    FirstViewController * first = [[FirstViewController alloc]init];    
    UINavigationController * nav = [[UINavigationControlleralloc]initWithRootViewController:first];  //UINavigationController第一個(gè)顯示的界面
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

2.隱藏導(dǎo)航欄工具欄

    self.navigationController.navigationBarHidden = YES;
    self.navigationController.toolbarHidden = NO;

3.設(shè)置導(dǎo)航欄和工具欄的顏色(設(shè)置了之后所有都是這個(gè)顏色,因?yàn)橹挥幸粋€(gè)navigationController)

    self.navigationController.toolbar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];

4.設(shè)置左右顯示的按鈕

    UIBarButtonItem * leftBtn = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleDonetarget:nil action:nil];
    self.navigationItem.leftBarButtonItem = leftBtn;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDonetarget:self action:@selector(gotoSecondView)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

5.設(shè)置標(biāo)題

    self.navigationItem.title =@"標(biāo)題";
    self.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFontsystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]};

6.標(biāo)題位置設(shè)置成view

    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.titleView = view;

7.Next按鈕響應(yīng)第二個(gè)界面

-(void)gotoSecondView{
    SecondViewController * svc = [[SecondViewController alloc]init];
    //設(shè)置默認(rèn)提供的返回按鈕的標(biāo)題
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;
    //present:在現(xiàn)有的界面上蓋上一層 dismissViewController來(lái)消除
    //[self.navigationController presentViewController:svc animated:YES completion:nil];
    //push:push入棧 pop出棧來(lái)消除
    [self.navigationController pushViewController:svc animated:YES];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,251評(píng)論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 25,571評(píng)論 7 249
  • 沒(méi)有期待的開(kāi)始, 和不曾預(yù)測(cè)的結(jié)束。 一切就如天空中的云朵, 仿佛全部是自然而然, 又卻像早已注定。 駐足,凝望,...
    芯滿亦足閱讀 328評(píng)論 0 4
  • 有時(shí)候,一時(shí)間就突然明白了什么!可停下來(lái),真正的追問(wèn)自己明白了什么?又說(shuō)不出來(lái),稀里糊涂,不知自己所云。只是有一種...
    遠(yuǎn)山不遠(yuǎn)閱讀 415評(píng)論 0 0
  • 我們?nèi)缤粋€(gè)個(gè)木偶, 生活在別人的操縱下! 我們?nèi)缤粋€(gè)個(gè)螺絲釘, 在社會(huì)這個(gè)機(jī)器上不斷運(yùn)行!
    石成朝閱讀 102評(píng)論 2 2