UINavigationControllernavigationController是一个用来表示不同视图之间从属关系的控件,navigationController通过维护一个栈,来实现不同具有从属(父子)关系的视图的切换。
以下是官方关于navigationController的使用
navigationController不属于控件,他继承于ViewController,是集合了navigationBar,navigationView等的控制器。
上图中,最左侧的是根视图,当点击其中的General时,General视图会滑入屏幕,用户继续点击Auto-Lock时,Auto-Lock视图会滑入屏幕。相应的,在对象的管理上,导航控制器使用了导航堆栈。根视图控制器在堆栈最底层,接下来入栈的是General视图控制器和Auto-Lock视图控制器。可以调用pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方法将视图控制器弹出堆栈。下面的图可以显示UINavigationControllerView的层级关系。
接下来我们来看UINavigationController的创建,在项目的AppDelegate.m上:
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
//创建UINavigationController
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewController];
//添加标题
self.viewController.navigationItem.title=@"导航栏";
//改变标题颜色
UIColor *cc=[UIColorredColor];
NSDictionary *dic=[NSDictionarydictionaryWithObject:ccforKey:UITextAttributeTextColor];
nav.navigationBar.titleTextAttributes=dic;
//添加标题,要么在AppDelegate里面如上面所写,要么直接在根视图的-(void)viewDidLoad上写上self.navigationItem.title=@"导航栏";都可以实现。
//改变背景颜色
nav.navigationBar.tintColor=[UIColor redColor];
//改变背景图片
[nav.navigationBarsetBackgroundImage:[UIImageimageNamed:@"13.jpg"] forBarMetrics:UIBarMetricsDefault];
//指定根视图
self.window.rootViewController = nav;
[self.windowmakeKeyAndVisible];
return YES;
}
显示效果看图:
当然要想让他实现视图之间的切换,我们必须自定义一个导航栏上的Butotn,在这个button的点击事件里面写上[self.navigationController pushViewController:av animated:YES];这段代码就可以了。看实例:
在根视图ViewController上写上:
- (void)viewDidLoad
{
[superviewDidLoad];
UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame=CGRectMake(200, 5,80, 34);
[btn1setTitle:@"下一页"forState:UIControlStateNormal];
[btn1addTarget:self action:@selector(xia:) forControlEvents:UIControlEventTouchUpInside];
//把自定义的button贴到导航条上
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:btn1];
}
-(void)xia:(UIButton *)sender{
//入栈
AViewController *av=[[AViewController alloc]init];
//下一个导航栏的标题和字体颜色
av.navigationItem.title=@"你好";
UIColor *cc=[UIColorredColor];
NSDictionary *dic=[NSDictionarydictionaryWithObject:ccforKey:UITextAttributeTextColor];
av.navigationController.navigationBar.titleTextAttributes=dic;
[self.navigationControllerpushViewController:avanimated:YES];
//模态视图控制器
// [self.navigationController presentViewController:av animated:YEScompletion:^{
// }];
//这样从下往上推出一个新的页面,没有之前的导航条
}
写之前记得导入相关的头文件,这样实现的效果就是:
一个是入栈前:
入栈后:
模态视图控制器
当然这里用的入栈是正常的push,出栈则是pop,还有另外一种叫模态视图控制器,它的作用:
临时中断当前工作流程,显示一个新的视图层次结构。
用途:
- 立即从用户那里收集信息;
- 临时显示一些内容;
- 临时改变工作模式;
- 为不同的设备方向实现可替代的界面;
- 使用指定类型的过渡动画来显示一个新的视图结构;
模态视图的显示风格:
通过设置属性modalpresentationStyle
- UIModalPresentationFullScreen;
- UIModalPresentationPageSheet;
- UIModalPresentationFormSheet;
模态视图的过渡风格:
通过设置属性modalTransitionStyle
- UIModalTransitionStyleCoverVertical;
- UIModalTransitionStyleFlipHorizontal;
- UIModalTransitionStyleCrossDissolve;
步骤:
- 创建一个要展示的视图控件;
- 在适当的地方分配一个委托对象;
- 调用当前视图控制前的presentModalViewController:animated:方法,传递你要模态显示的视图控制器;
- 当要出栈时得用dismissModalViewControllerAnimated:YES 这个方法
以上这个就是对模态视图的简单介绍。
页签视图tabBarController控制器
它的使用范围是当你的程序分为几个相对独立的部分时,就比较适合使用tabBarController来组织界面。
此时,如图,屏幕下方有四个按钮,单击不同的按钮进入不同的界面,每个界面相对来说在整个系统中比较独立,也就是程序的四个相对独立的的不同部分,在每个相对独立的部分你也可以使用UINavigationController等容器类组织你的界面。这样组织使程序逻辑非常清晰,当然你也可以组织多个tabBar而不仅仅是四个。接下来看如何创建。
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//Override point for customization after applicationlaunch.
self.viewController = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
self.viewController.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"NAV"image:[UIImage imageNamed:@"Airplane"] tag:0];
self.viewController.navigationItem.title=@"NAV";//设置导航栏的标题
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.viewController];
[nav.navigationBarsetBackgroundImage:[UIImageimageNamed:@"13.jpg"] forBarMetrics:UIBarMetricsDefault];//设置导航控制器的背景图片
UITabBarController *tabBarCler=[[UITabBarControlleralloc]init];
CViewController *c=[[CViewController alloc]init];
DViewController *d=[[DViewController alloc]init];
EViewController *e=[[EViewController alloc]init];
c.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"C"image:[UIImage imageNamed:@"ATM-Card"] tag:1];//设置tabBar的图标和title
c.navigationItem.title=@"C";//设置导航栏的标题
UINavigationController *nc=[[UINavigationController alloc]initWithRootViewController:c];
d.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"D"image:[UIImage imageNamed:@"Tools"] tag:2];//设置tabBar的图标和title
d.navigationItem.title=@"D";//设置导航栏的标题
UINavigationController *nd=[[UINavigationController alloc]initWithRootViewController:d];
e.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"E"image:[UIImage imageNamed:@"Unlocked"] tag:3];//设置tabBar的图标和title
e.navigationItem.title=@"E";//设置导航栏的标题
UINavigationController *ne=[[UINavigationController alloc]initWithRootViewController:e];
NSArray*aArray=@[nav,nc,nd,ne];
tabBarCler.viewControllers=aArray;
self.window.rootViewController = tabBarCler;
[self.windowmakeKeyAndVisible];
return YES;
}
这样就实现了以上的效果,当然由于我这个例子里面套了很多的视图,第一个视图和后面三个视图的导航栏颜色是分别设置的,所以不一样,你们有兴趣的话可以自己试试。好了,就总结到这里!