UINavigationController 导航控制器
//
// ZKAppDelegate.m
// 01-UINavigationController的创建
//
#import "ZKAppDelegate.h"
#import "ZKFirstViewController.h"
@implementation ZKAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//创建一个ZKFirstViewController的对象
ZKFirstViewController *first = [[ZKFirstViewController alloc]init];
// //将first类添加为程序的根视图控制器
// self.window.rootViewController = first;
//创建导航控制器,并将first作为导航控制器的根视图控制器
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:first];
//将导航控制器作为程序的根视图控制器
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
//
// ZKFirstViewController.m
// 01-UINavegationControllerDemo创建
//
#import "ZKFirstViewController.h"
#import "ZKSecondViewController.h"
@interface ZKFirstViewController ()
@end
@implementation ZKFirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeSystem];
secondButton.frame = CGRectMake(100, 100, 120, 40);
[secondButton setTitle:@"到第二页" forState:UIControlStateNormal];
[secondButton addTarget:self action:@selector(nextController:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:secondButton];
//数据显示过程:先将数据保存到navigationItem的相关属性当中,再由navigtionBar来显示navigationItem里面的数据,每个视图控制器都对应着一个navigationItem,但是navigtionBar只有一个。到那个界面,就将那个界面对应的navigationItem里面的数据显示到navigtionBar上面。
//1.设置标题
//1.1 直接设置
self.title = @"第一页";
//1.2 通过导航控制器设置
//注意:两种方式效果相同
self.navigationItem.title = @"第一页";
//2.设置标题视图
//注意:titleView与title是同一个位置,同时存在时,titleView会将title覆盖
UIImage *image = [UIImage imageNamed:@"logo_title"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
self.navigationItem.titleView = imageView;
//2.1副标题(详细信息)
// self.navigationItem.prompt = @"副标题";
//3.添加左边的barItem(可以看做特殊的按钮)
//3.1 用户自定义标题的barItem
UIBarButtonItem *leftItem1 = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemClicked:)];
//3.2 系统的barItem
UIBarButtonItem *leftItemSys = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftItemClicked:)];
//3.3 用户自定义的barItem(通过按钮设置)
//怎样将按钮(UIbutton对象)添加到导航栏的左边?
//1.创建UIButton
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//设置title
[leftButton setTitle:@"按钮添加" forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(leftItemClicked:) forControlEvents:UIControlEventTouchUpInside];
//设置背景图片
UIImage *leftImage = [UIImage imageNamed:@"main_left_nav"];
[leftButton setBackgroundImage:leftImage forState:UIControlStateNormal];
//自定义的按钮要设置frame
leftButton.frame = CGRectMake(0, 0, leftImage.size.width, leftImage.size.height);
//2.通过UIBarButtonItem加载按钮
UIBarButtonItem *leftItem2 = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
//设置一个item
//self.navigationItem.leftBarButtonItem = leftItem2;
//设置一组item
self.navigationItem.leftBarButtonItems = @[leftItem1,leftItem2,leftItemSys];
//3.添加右边的barItem(可以看做特殊的按钮)
UIImage *imageRight = [UIImage imageNamed:@"main_right_nav"];
//创建按钮,用于显示在navigationBar的右边
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setBackgroundImage:imageRight forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(rightItemClicked:) forControlEvents:UIControlEventTouchUpInside];
rightButton.frame = CGRectMake(0, 0, imageRight.size.width, imageRight.size.height);
//通过按钮设置右边的item
//添加右边的item
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightItem;
//在右边添加一组item
//self.navigationItem.rightBarButtonItems
//5.设置返回item,target与action设成空即可
UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem;
//6.设置navigationBar的属性
//6.1设置bar的类型
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
//6.2设置颜色格调(混合色)
[self.navigationController.navigationBar setBarTintColor:[UIColor blueColor]];
//状态栏高度:20 导航栏高度:44 工具栏:40?
//6.3设置背景图片
//注意:添加背景图片的大小 是320*64 或320*44(隐藏状态栏)
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header_bg"] forBarMetrics:UIBarMetricsDefault];
//6.4.隐藏导航栏
//self.navigationController.navigationBar.hidden = YES;
//[self.navigationController.navigationBar setHidden:YES];
//7 工具栏的设置
//7.1 显示工具栏
// [self.navigationController.toolbar setHidden:NO];
self.navigationController.toolbarHidden = NO;
//作用:可以添加一系列的item,实现功能
//创建几个item
UIBarButtonItem *toolBarItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(toolItemClicked:)];
UIBarButtonItem *toolBarItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *toolBarItem3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(toolItemClicked:)];
UIBarButtonItem *toolBarItem4 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *toolBarItem5 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(toolItemClicked:)];
//注意:不要使用了
// self.navigationController.toolbarItems = @[toolBarItem1];
//使用下面这种进行添加item
self.toolbarItems = @[toolBarItem1,toolBarItem2,toolBarItem3,toolBarItem4,toolBarItem5];
//给工具栏添加背景图片
//背景图大小:320*44
[self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"header_bg"] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
//工具栏高度:44
NSLog(@"%lf",self.navigationController.toolbar.frame.size.height);
// Do any additional setup after loading the view.
}
-(void)toolItemClicked:(UIBarButtonItem *)item{
NSLog(@"这里是toolBarItem");
}
-(void)rightItemClicked:(UIBarButtonItem *)item{
NSLog(@"进行右边添加了");
}
-(void)leftItemClicked:(UIBarButtonItem *)item{
NSLog(@"进行左边添加了");
}
-(void)nextController:(UIButton *)button{
//创建第二页的对象
ZKSecondViewController *second = [[ZKSecondViewController alloc]init];
// //模态跳转
// [self presentViewController:second animated:YES completion:nil];
//利用导航进行跳转
//解释:self.navigationController就是本界面所在的导航控制器
//pushViewController:执行的是入栈操作
[self.navigationController pushViewController:second animated:YES];
}