UINavigationController

 1 #import "AppDelegate.h"
 2 #import "RootViewController.h"
 3 @interface AppDelegate ()
 4
 5 @end
 6
 7 @implementation AppDelegate
 8
 9
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     // Override point for customization after application launch.
12 #pragma mark - UINagationController的创建
13     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
14     self.window.backgroundColor = [UIColor greenColor];
15     [self.window makeKeyAndVisible];
16     //1.创建一个根试图控制器
17     RootViewController *rootVc = [[RootViewController alloc] init];
18     //2.创建导航控制器,将rootVC作为其根视图控制器,初始化必须有一个根视图控制器
19     UINavigationController *naVc = [[UINavigationController alloc] initWithRootViewController:rootVc];
20     //3.将naVC作为window的根视图控制器
21     self.window.rootViewController = naVc;
22     naVc.delegate = self;
23     //NSLog(@"%@",naVc);
24
25     return YES;
26 }
27 #pragma mark - UINavigationControllerDelegate 代理方法
28 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
29 {
30     NSLog(@"%s",__FUNCTION__);
31     //navigationConroller的几个属性
32     NSLog(@"view controllers = %@",navigationController.viewControllers);
33     NSLog(@"top viweController = %@",navigationController.topViewController);
34     NSLog(@"visiableController = %@",navigationController.visibleViewController);
35 }
36 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
37
38 }

2.

  1 #import "RootViewController.h"
  2 #import "SencondViewController.h"
  3 @interface RootViewController ()
  4
  5 @end
  6
  7 @implementation RootViewController
  8
  9 - (void)viewDidLoad {
 10     [super viewDidLoad];
 11     // Do any additional setup after loading the view.
 12     self.view.backgroundColor = [UIColor redColor];
 13 #pragma mark - UINavigationBar(导航栏)的设置
 14     /*
 15      设置导航栏的样式barStyle和透明度translucent
 16      */
 17 //    NSLog(@"%@",self.navigationController);
 18 //    //导航栏默认的颜色是白色半透明状态
 19 //    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
 20 //    self.navigationController.navigationBar.translucent = YES;
 21 //    //第二种 白色不透明
 22 //    self.navigationController.navigationBar.barStyle = 0;
 23 //    self.navigationController.navigationBar.translucent = NO;
 24 //    //第三种 黑色,透明
 25 //    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
 26 //    self.navigationController.navigationBar.translucent = YES;
 27 //    //第四种 黑色, 不透明
 28 //    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
 29 //    self.navigationController.navigationBar.translucent = NO;
 30     /*
 31       设置导航栏的颜色:barTintColor
 32      注意;barTintColor是iOS7以后的新特性,此修改不了导航栏的透明度,如果想修改导航栏的透明度,使用backgroundcolor
 33
 34
 35      */
 36     //self.navigationController.navigationBar.barTintColor = [UIColor brownColor];
 37     self.navigationController.navigationBar.backgroundColor = [UIColor brownColor];
 38     /*
 39      tintColor设置导航栏控件的颜色,默认的是蓝色
 40      */
 41     self.navigationController.navigationBar.tintColor = [UIColor blueColor];
 42     /*
 43      设置导航栏的背景图片:导航栏的竖屏高度是44;横屏的是36
 44      像素图片:64,44和小于44的(要分清是否是一倍像素的)
 45      */
 46     //一倍像素为:64  =  导航栏 + 状态栏
 47 //    UIImage *image = [UIImage imageNamed:@"NavBar_64"];
 48 //    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
 49 //    //一倍像素为:44 = 导航栏
 50 //    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_44"] forBarMetrics:UIBarMetricsDefault];
 51 //    //一倍像素为:小于44,这是不被允许的情况
 52 //    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_30"] forBarMetrics:UIBarMetricsDefault];
 53
 54 #pragma mark - 导航栏内容的设置UINavigationItem
 55     //1.在工程里有且仅有一个UINavigationController,导航栏UINavigationBar也是有且仅有一个
 56     //2.导航栏的所有内容基本上都在UINavigationItem
 57     //3.每一个视图控制器都有一个UINavigationItem
 58
 59
 60     //设置UINavigationItem的四种方式
 61     //第一种方式:系统默认的
 62 //    UIBarButtonItem  *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftBarButtonAction)];
 63 //    self.navigationItem .leftBarButtonItem = leftItem;
 64
 65     //第二种方式:使用title初始化的方法
 66 //    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction)];
 67 //    self.navigationItem.leftBarButtonItem = leftItem;
 68     /*
 69     //第三种方式:使用image初始化[此时图片没有保持原图片样式,因为默认是系统原模板样式,此时要实现效果需要对图片进行渲染]
 70     UIImage *image1 = [UIImage imageNamed:@"NavBtnLeft"];
 71     //渲染之后,使用新的UIImage对象去接收
 72     UIImage *image2 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 73     UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction)];
 74     self.navigationItem.rightBarButtonItem = rightItem;
 75     //第四种方式:使用视图进行初始化
 76     UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
 77     UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
 78     self.navigationItem.leftBarButtonItem = leftItem;
 79
 80     */
 81     //使用多个item进行赋值
 82     UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(leftBarButtonAction)];
 83     UIBarButtonItem *item2 =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(leftBarButtonAction)];
 84     self.navigationItem.leftBarButtonItems = @[item,item2];
 85
 86     //使用多个item进行赋值
 87     UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(leftBarButtonAction)];
 88     UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(leftBarButtonAction)];
 89     self.navigationItem.rightBarButtonItems = @[item3, item4];
 90
 91
 92
 93     //中间的一块title和titleView
 94     //self.title = @"首页";
 95    // self.navigationItem.title = @"首页";
 96
 97     //使用View初始化titleView
 98     self.navigationItem.titleView = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"电话"]];
 99     //隐藏导航栏,默认的为NO
100     //self.navigationController.navigationBarHidden = YES;
101
102     //设置相关Controller的侧滑手势,navigationController作为根视图控制器才会有侧滑效果,默认为开,可以设置进行关闭
103     self.navigationController.interactivePopGestureRecognizer.enabled = NO;
104
105 #pragma mark - 页面跳转
106     //创建页面跳转的button
107     UIButton *pushNextControllerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
108     pushNextControllerBtn.frame = CGRectMake(100, 100, 100, 100);
109     [pushNextControllerBtn setTitle:@"push" forState:UIControlStateNormal];
110     [pushNextControllerBtn addTarget:self action:@selector(pushButtonAction) forControlEvents:UIControlEventTouchUpInside];
111     [self.view addSubview:pushNextControllerBtn];
112
113 #pragma mark - 模态视图
114     UIButton *modelVVBtn = [UIButton buttonWithType:UIButtonTypeCustom];
115     modelVVBtn.frame = CGRectMake(100, 300, 100, 100);
116     [modelVVBtn setTitle:@"model" forState:UIControlStateNormal];
117     [modelVVBtn addTarget:self action:@selector(modelViewControllerAction) forControlEvents:UIControlEventTouchUpInside];
118     [self.view addSubview:modelVVBtn];
119
120
121
122 }
123 #pragma mark - 模态出下一个界面
124 -(void)modelViewControllerAction
125 {
126     //1.创建一个需要模态的下一个Controller的对象
127     SencondViewController *secondVC = [[SencondViewController alloc] init];
128
129     //2.设置转场动画
130     secondVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
131
132     //3.模态到下一个界面
133     //参数1:模态将要出现的controller 参数2:动画 参数3:模态完成需要处理的事件
134     [self presentViewController:secondVC animated:YES completion:^{
135
136     }];
137 }
138 #pragma mark - pushButton事件
139 -(void)pushButtonAction
140 {
141      //创建要跳转的controller
142     SencondViewController *secondVC = [[SencondViewController alloc] init];
143     //利用导航控制器去退出下一个界面
144     [self.navigationController pushViewController:secondVC animated:NO];
145
146 }
147 #pragma mark - 左侧button的方法
148 - (void)leftBarButtonAction
149 {
150
151
152 }
 1 #import "SencondViewController.h"
 2 #import "RootViewController.h"
 3 #import "ThirdViewController.h"
 4 @interface SencondViewController ()
 5
 6 @end
 7
 8 @implementation SencondViewController
 9
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view.
13     //注意了:在要推出的界面中,需要修改视图的背景颜色,用于解决跳转界面形似卡顿的一个现象,增加用户体验
14     self.view.backgroundColor = [UIColor blueColor];
15
16     //创建返回按钮
17     UIButton *popViewController = [UIButton buttonWithType:UIButtonTypeSystem];
18     popViewController.frame = CGRectMake(100, 100, 100, 100);
19     [popViewController setTitle:@"返回" forState:UIControlStateNormal];
20     [popViewController addTarget:self action:@selector(popVCAction) forControlEvents:UIControlEventTouchUpInside];
21     [self.view addSubview:popViewController];
22
23     //创建PUSH按钮
24     UIButton *pushViewController = [UIButton buttonWithType:UIButtonTypeSystem];
25     pushViewController.frame = CGRectMake(100, 300, 100, 100);
26     [pushViewController setTitle:@"push" forState:UIControlStateNormal];
27     [pushViewController addTarget:self action:@selector(pushViewAction) forControlEvents:UIControlEventTouchUpInside];
28     [self.view addSubview:pushViewController];
29     //创建一个模态返回的按钮
30     UIButton *dismissViewController = [UIButton buttonWithType:UIButtonTypeSystem];
31     dismissViewController.frame = CGRectMake(100, 500, 100, 100);
32     [dismissViewController setTitle:@"push" forState:UIControlStateNormal];
33     [dismissViewController addTarget:self action:@selector(dismissViewAction) forControlEvents:UIControlEventTouchUpInside];
34     [self.view addSubview:dismissViewController];
35
36 }
37 //模态返回上一层级
38 -(void)dismissViewAction
39 {
40     [self dismissViewControllerAnimated:YES completion:^{
41
42     }];
43     //push适用于有层级关系一系列视图的跳转,它有层次递进关系,模态仅用于单独功能模块的跳转,不牵扯到主业务流程(登录,注册,拍照,相机)
44 }
45 //返回上一级页面的响应方法
46 -(void) popVCAction
47 {
48 //    //1.返回根视图控制器
49 //    [self.navigationController popToRootViewControllerAnimated:YES];
50 //    //2.返回上一层
51 //    [self.navigationController popViewControllerAnimated:YES];
52     //3.返回到指定的层级 [新手易犯的错,返回指定的Controller不应该创建,应该去桟管理的视图中寻找]
53     //通过打印当前视图控制器所控制的视图,可以找到相应视图的下标
54     NSLog(@"%@",self.navigationController.viewControllers);
55      RootViewController *rootVC = [self.navigationController.viewControllers objectAtIndex:0];
56     [self.navigationController popToViewController:rootVC animated:YES];
57 }
58 - (void)didReceiveMemoryWarning {
59     [super didReceiveMemoryWarning];
60     // Dispose of any resources that can be recreated.
61 }
62 //  推出下一层页面的方法
63 -(void)pushViewAction
64 {
65     ThirdViewController *thirdView = [[ThirdViewController alloc] init];
66     thirdView.view.backgroundColor = [ UIColor magentaColor];
67     UIButton *popViewControllerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
68     [popViewControllerBtn setTitle:@"pop" forState:UIControlStateNormal];
69     popViewControllerBtn.frame = CGRectMake(100, 100, 100, 100);
70     [popViewControllerBtn addTarget:self action:@selector(popViewAction) forControlEvents:UIControlEventTouchUpInside];
71     [thirdView.view addSubview:popViewControllerBtn];
72     [self.navigationController pushViewController:thirdView animated:NO];
73
74 }
75 -(void)popViewAction
76 {
77     NSLog(@"viewControllers = %@",self.navigationController.viewControllers);
78     UIViewController *viewController = [self.navigationController.viewControllers objectAtIndex:1];
79     [self.navigationController popToViewController:viewController animated:NO];
80 }
时间: 2024-12-15 01:37:39

UINavigationController的相关文章

iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

转自:http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigationController可以翻译为导航控制器,在iOS里经常用到. 我们看看它的如何使用: 下面的图显示了导航控制器的流程.最左侧是根视图,当用户点击其中的General项时 ,General视图会滑入屏幕:当用户继续点击Auto-Lock项时,Auto-Lock视图将滑入屏幕.相应地,在对象

ios UINavigationController

转自:http://www.cnblogs.com/ios8/p/ios-UINavigationController.html UINaviGationController通常被我们称为导航栏,他是视图与视图之间联系沟通的桥梁,一些著名的app都用到了他.下面我们来看一下如何建立一个navigation. 首先,我们通常新建工程是直接将视图控制器添加到window上,而现在有navigation以后,就多了一层:  Appdelegete.h: - (BOOL)application:(UIA

UINavigationController和UIBarButtonItem的例子

#import "AppDelegate.h" #import "FirstViewController.h" #import "SecondViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOption

iOS设计之 多视图导航栏UINavigationController切换视图的简单设计

在iOS平台上创建有个工程,之后在工程中创建两个类视图 操作步骤如下 1.在分别在两个类视图中对主视图设置背景色 FirstViewController.m #import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad {    [super viewDidLoad];        //设置主视

UINavigationController导航控制器

UINavigationController导航控制器,是多个界面间跳转的重要元素,可以理解为它存储着多个viewController,它的存储结构是栈,栈的特点是先进后出,所以添加视图控制器时,要特别注意. UINavigationController有几个常用的方法: 1:[UINavigationController initWithRootViewController:viewController ] 是指将哪一个视图控制器设置为导航控制器的根视图控制器,即运行是第一次看到的界面. 2:

UINavigationController使用的注意事项

1.常用属性viewControllers //所有在栈中的控制器topViewController //栈顶控制器navigationBar //导航栏 竖屏下默认44,横屏默认32 2.对navigationBar的标题进行字体颜色等设置NSDictionary *dic = @{ NSForegroundColorAttributeName:[UIColor whiteColor] }; UIViewController.navigationBar.titleTextAttributes

UINavigationController使用详解

有一阵子没有写随笔,感觉有点儿手生.一个多月以后终于又一次坐下来静下心写随笔,记录自己的学习笔记,也希望能够帮到大家. 废话少说回到正题,UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的APP中(如qq,系统相册等)都有 用到.说是使用详解,其实我只会介绍几个自认为比较重要或者容易放错的地方进行讲解,下面让我们挨个探探究竟: 首先上一张图(来自苹果官

UINavigationController 、界面通信

一.UINavigationController 二.定制UINavigationBar 三.界面间通信 一.UINavigationController 导航控制器,是iOS中最常用的多视图控制器之一,它用来管多个视图控制器.导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器. UINavigationController继承于UIViewController,以栈的方式管理所控制的视图控制器, 至少要有一个被管理的视图控制器,这个控制器我们称作,导航控制器的根视图控制器. 任

iOS架构-UINavigationController那些事

1. UINavigationController提供的功能 导航控制器提供在不同页面之间的导航功能, 包括: push back. 统一的导航栏. 导航的手势(侧滑后退) 统一的工具栏 导航控制器 导航控制器维护一个堆栈结构. 一层一层. 入栈操作对应push, 但是不会销毁之底部的控制器 出栈操作对应back, 会销毁顶部的控制器(减少一个引用, 如果你想保留也可以) back操作会很快, 因为不需要创建和加载视图.