IOS ——UI篇——UINavigationController导航控制器的使用

UINavigationController
 
 
 
UINavigationController俗称导航控制器,可以看做是一个试图控制器的容器,主要用于试图控制器之间的沟通,一般作为根视图控制器使用
  1. 创建:在appdelegate里的didfinishlaunsh方法里创建,创建的同时需要给UINavigationController设置艮视图控制器:
//首先创建自己的视图控制器
ViewController *vc = [[ViewController alloc]init];
//然后创建导航控制器,并且将上一步创建的ViewController对象设置为导航控制器的根控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//最后将导航控制器设置为窗口的根控制器
self.window.rootViewController = nav;
  1. self.title = @"导航";//设置导航条在某试图控制器中的标题(在对应视图控制器中设置)
  2. self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil];//修改标题颜色
  3. self.navigationController.navigationBar.translucent = NO;//设置导航条半透明状态,默认为yes,会遮挡住self.view的64像素的高度
  4. [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];//设置navigationbar的颜色
  5. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:UIBarMetricsDefault];//设置背景图片(注意尺寸)
  6. self.navigationController.navigationBarHidden = YES;//隐藏导航条
  [self.navigationController setNavigationBarHidden:YES animated: YES];//隐藏导航条,是否带动画
  1. [self.navigationController pushViewController:sec animated:YES];//推到下一个视图
  2. [self.navigationController popViewControllerAnimated:YES];//返回上一级视图
  3. [self.navigationController popToRootViewControllerAnimated:YES];//退回到根视图
  4. (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; //返回指定视图
  5. UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:0];//获得导航控制器的viewcontrollers栈
  6. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"返回"  style:UIBarButtonItemStylePlain  target:self  action:nil];//设置导航条返回按钮的标题
self.navigationItem.backBarButtonItem = backButton;
  1. //自定义返回按钮:
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 10, 40, 40);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = left;
//还可以initWithImage初始化成图片
    //还可以自定义,可以是任意一个UIView
    UIBarButtonItem *barBtn2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(changeColor2)];
    UIBarButtonItem *barBtn3=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"logo-40@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(changeColor3)];
    UIView *view4=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    view4.backgroundColor=[UIColor blackColor];
    UIBarButtonItem *barBtn4=[[UIBarButtonItem alloc]initWithCustomView:view4];
    NSArray *arr1=[[NSArray alloc]initWithObjects:barBtn2,barBtn3,barBtn4, nil];
    self.navigationItem.rightBarButtonItems=arr1;
 
  1. //自定义titleView
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2"]];
seg.frame = CGRectMake(0, 10, 80, 20);
self.navigationItem.titleView = seg;
 
注意:
  1.navigationController 和  navigationItem这两个属性是平级的所以直接可以用self.navigationItem
  2. 用self.title = @"公交路线查询";方法设置的标题会受标签标题的影响;
[self.navigationItem setTitle:@"公交路线查询"];//设置导航条标题(如果使用了tableBar,这种方式设置的标题不会受标签的标题的影响)
self.navigationItem.title = @"宝妈圈";//设置导航条标题(如果使用了tableBar,这种方式设置的标题不会受标签的标题的影响)
 
应用举例:
 
  1 #import "AppDelegate.h"
  2 #import "ViewController.h"
  3 @interface AppDelegate ()
  4 
  5 @end
  6 
  7 @implementation AppDelegate
  8 
  9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 10 
 11     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
 12     [self.window makeKeyAndVisible];
 13 
 14     ViewController *vc = [[ViewController alloc] init];//导航控制器的根视图
 15 
 16     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];//初始化导航控制器
 17 
 18     self.window.rootViewController = nav;//设置window的根视图控制器为导航控制器
 19 
 20     return YES;
 21 }
 22 
 23 #import "ViewController.h"
 24 #import "SecondViewController.h"
 25 @interface ViewController ()
 26 
 27 @end
 28 
 29 @implementation ViewController
 30 
 31 -(void)viewWillAppear:(BOOL)animated{
 32     [super viewWillAppear:animated];
 33 
 34 }
 35 -(void)hidden{
 36 
 37    // [self.navigationController setNavigationBarHidden:YES animated:NO];
 38 }
 39 - (void)viewDidLoad {
 40     [super viewDidLoad];
 41     self.view.backgroundColor = [UIColor brownColor];
 42 
 43     //延迟调用
 44     [self performSelector:@selector(hidden) withObject:nil afterDelay:1];
 45 
 46     //导航条默认半透明,这时会遮挡self.view的64个像素的高度(tableView除外),为了避免这种情况,我们可以将导航条的半透明效果关掉
 47     self.navigationController.navigationBar.translucent = NO;//将导航条的半透明效果关掉
 48 
 49     self.title = @"导航条";//设置导航条标题
 50 [self.navigationItem setTitle:@"导航条"];//设置导航条标题(如果使用了tableBar,这种方式设置的标题不会受标签的标题的影响)
 51 self.navigationItem.title = @"导航条";//设置导航条标题(如果使用了tableBar,这种方式设置的标题不会受标签的标题的影响)
 52     self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil];//修改标题颜色
 53 
 54     self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];//导航条背景色修改
 55    //给导航条增加背景图片,其中forBarMetrics有点类似于按钮的for state状态,即什么状态下显示
 56     //UIBarMetricsDefault-竖屏横屏都有,横屏导航条变宽,则自动repeat图片
 57     //UIBarMetricsCompact-竖屏没有,横屏有,相当于之前老iOS版本里的UIBarMetricsLandscapePhone
 58     //UIBarMetricsCompactPrompt和UIBarMetricsDefaultPrompt暂时不知道用处,官方解释是Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar,以后遇到时再细说
 59     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"big2.png"] forBarMetrics:UIBarMetricsDefault];
 60     //如果图片太大会向上扩展侵占状态栏的位置,在状态栏下方显示
 61     //clipsToBounds就是把多余的图片裁剪掉
 62     UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 64)];
 63     la.backgroundColor = [UIColor blackColor];
 64     [self.view addSubview:la];
 65 
 66     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 67     [btn setTitle:@"跳转" forState:UIControlStateNormal];
 68     btn.frame = CGRectMake(20, 100, 80, 80);
 69     btn.backgroundColor = [UIColor whiteColor];
 70     [btn addTarget:self action:@selector(gotoNext) forControlEvents:UIControlEventTouchUpInside];
 71     [self.view addSubview:btn];
 72 }
 73 -(void)gotoNext{
 74 
 75     SecondViewController *secondVc = [[SecondViewController alloc] init];
 76     //nav 跳转方式
 77     [self.navigationController pushViewController:secondVc animated:YES];
 78 
 79 }
 80 - (void)didReceiveMemoryWarning {
 81     [super didReceiveMemoryWarning];
 82     // Dispose of any resources that can be recreated.
 83 }
 84 
 85 @end
 86 
 87 //[self.navigationItem setHidesBackButton:YES];隐藏导航条上的返回按钮
 88  //自定义导航条左按钮
 89     UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 90     [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
 91     backBtn.frame = CGRectMake(0, 0, 30, 40);
 92 
 93     [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
 94     UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
 95     self.navigationItem.leftBarButtonItem = item;
 96 
 97     // UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
 98 
 99     //自定义导航条右按钮
100     UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
101     [rightBtn setImage:[UIImage imageNamed:@"chatWith"] forState:UIControlStateNormal];
102     rightBtn.frame = CGRectMake(0, 0, 40, 30);
103     [rightBtn addTarget:self action:@selector(chat) forControlEvents:UIControlEventTouchUpInside];
104 
105     UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
106 
107     self.navigationItem.rightBarButtonItem = right;
108 112 
113 UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2"]];
114     seg.frame = CGRectMake(0, 0, 80, 40);
115 
116     self.navigationItem.titleView = seg;//自定义导航条头视图
117 
118     self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
119 
120 -(void)backToHome{
121    // [self.navigationController popToRootViewControllerAnimated:YES];//返回到nav的根视图
122 
123     //nav中所存放的vc对象数组
124     NSArray *arr = self.navigationController.viewControllers;
125     NSLog(@"==%@",arr);
126 
127     UIViewController *vc = [arr objectAtIndex:2];
128 
129     //跳转到指定VC对象,此VC必须存在于NAV的栈中
130     [self.navigationController popToViewController:vc animated:YES];
131 
132 }
133 
134 //视图将要出现时调用的方法
135 -(void)viewWillAppear:(BOOL)animated{
136     [super viewWillAppear:animated];
137     NSArray *arr = self.navigationController.viewControllers;//获取导航控制器中的 视图控制器
138     NSLog(@"==%@",arr);
139 
140 }

 

 
posted @ 2015-04-22 13:09  #零下一度&  阅读(374)  评论(0编辑  收藏  举报