您的位置:首页 > 移动开发 > Objective-C

#Objective - C - UI-design - 第七天 -UIKit框架-UIKit-导航控制器-UINavigationController

2016-01-11 09:32 866 查看

UINavigationController

UINavigationController:导航控制器,是iOS中最常用的多视图控制器之一,用它来管理多个视图控制器。

导航控制器可以称为是,管理控制器的控制器,主要管理有层 次递进关系的控制器。

效果:



UINavigationController继承于UIViewController以栈的方式管理所控制的视图控制器,至少要有一个被管理的视图控制器:这个控制器我们称作,导航控制器的根视图控制器。

任何继承⾃自UIViewController的类(多态)都可以作为根控制器。

创建(Delegate.m下)

// 创建根视图
RootViewController *rootVC = [[RootViewController alloc] init];
// 创建导航控制器 把rootVC作为导航控制器的根视图控制器
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:rootVC];
// 设置导航为window的根视图
self.window.rootViewController = navi;
// 内存管理
[rootVC release]; [navi release];




UINavigationBar

UINavigationBar(导航栏)上的设置主要分两部分,一为导航栏上的各种导航部件(UINavigationItem),二为导航栏自身的相关设置。

UINavigationBar—导航条,iOS7之后默认是半透明的,iOS7之前默认是不透明的

UINavigationBar竖屏下默认高度44,横屏下默认高度32。

iOS7之后,UINavigationBar的背景会延伸到statusBar上。导航栏高度仍保持44,但显⽰示效果为64。

每个视图控制器都有一个UINavigationItem属性UINavigationItem中设置的左按钮、右按钮、标题等,会随着控制器的显示,也显示到UINavigationBar上。

初始化

// 导航栏标题
self.title = @"标题";
// self.title会同时改变导航栏的标题和tabBar的标题 可以用如下方法单独操作导航栏标题
self.navigationItem.title = @"标题";
// 左按钮
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(leftAction)] autorelease];
// 右按钮
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage: [UIImage imageNamed:@"next"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];




属性:

// 导航栏的显隐属性
self.navigationController.navigationBarHidden = NO;
// 导航栏样式
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
// 背景颜色
self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
// 导航栏颜色
self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];
// 导航栏元素颜色
self.navigationController.navigationBar.tintColor = [UIColor blackColor];




UINavigationItem

// 按钮对象
UIBarButtonItem *item1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftAction)] autorelease];
UIBarButtonItem *item2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(leftAction)] autorelease];
// 左按钮数组
self.navigationItem.leftBarButtonItems = @[item1, item2];
// 右按钮数组
self.navigationItem.rightBarButtonItems = @[item1, item2];




TitleView标题视图

// 标题视图 titleView

// 创建要添加的视图对象
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"已接来电",@"未接来电"]];
// 设置属性
seg.frame = CGRectMake(0, 0,100, 30);
seg.selectedSegmentIndex = 0; // 放在titleView上
self.navigationItem.titleView = seg;
[seg release];




// 当半透明效果开启时 屏幕左上⾓角为坐标原点
// 关闭时 导航栏左下⾓角为坐标原点
self.navigationController.navigationBar.translucent = YES;




导航常用属性



入栈和出栈



//进入下一页

- (void)next
{
// 页面跳转
// 推出(push)
// 1.创建第二页对象
SecondViewController *secVC = [[SecondViewController alloc] init];
// 2.通过导航控制器推出新的⻚页⾯面
[self.navigationController pushViewController:secVC animated:YES];
// 3.内存管理
[secVC release];
}

//返回上一页

- (void)back
{
[self.navigationController popViewControllerAnimated:YES];
}
// VC数组
NSLog(@"%@", self.navigationController.viewControllers); // 返回指定的VC
[self.navigationController popToViewController:self.navigationController.viewControllers[0]
animated:YES];
// 返回根视图
[self.navigationController popToRootViewControllerAnimated:YES];


小练习







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 导航条