美文网首页
UINavigationController的简单介绍

UINavigationController的简单介绍

作者: Mustard_Buli | 来源:发表于2016-03-16 15:33 被阅读39次

UINavigationController是IOS编程中比较常用的一种容器view controller,也就是导航栏控制器。

     (这里是文档里的图片)

1.创建一个UINavigationController

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];   
    FirstViewController * first = [[FirstViewController alloc]init];    
    UINavigationController * nav = [[UINavigationControlleralloc]initWithRootViewController:first];  //UINavigationController第一个显示的界面
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];

2.隐藏导航栏工具栏

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

3.设置导航栏和工具栏的颜色(设置了之后所有都是这个颜色,因为只有一个navigationController)

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

4.设置左右显示的按钮

    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.设置标题

    self.navigationItem.title =@"标题";
    self.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFontsystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]};

6.标题位置设置成view

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

7.Next按钮响应第二个界面

-(void)gotoSecondView{
    SecondViewController * svc = [[SecondViewController alloc]init];
    //设置默认提供的返回按钮的标题
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;
    //present:在现有的界面上盖上一层 dismissViewController来消除
    //[self.navigationController presentViewController:svc animated:YES completion:nil];
    //push:push入栈 pop出栈来消除
    [self.navigationController pushViewController:svc animated:YES];
}

相关文章

网友评论

      本文标题:UINavigationController的简单介绍

      本文链接:https://www.haomeiwen.com/subject/uxwqlttx.html