标签:
UINavigationController是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图。 任何类型的视图控制器都可放入栈中。在设计导航控制器时需要指定根视图即用户看到的第一个视图。根视图控制器是被导航控制器推入到栈中的第一个视图控制 器。当用户查看下一个试图时,栈中将加入一个新的视图控制器,它所控制的视图将展示给用户。我们可以通过导航按钮来操作分层的应用程序,用它来控制视图的 推入或推出。
#import "MainViewController.h" #import "SecondViewController.h" @interface MainViewController ()<UINavigationControllerDelegate,UINavigationBarDelegate> @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏标题,上下的标题都变(tabbar的也改变) // self.title = @"首页"; //只有导航栏的标题改变 self.navigationItem.title = @"Hello"; //标题上面的小标题(导航栏高度会改变) // self.navigationItem.prompt = @"Hello!";
//导航栏隐藏 导航栏是唯一的
// self.navigationController.navigationBarHidden = YES ;
//导航栏的背景颜色,如果设置颜色,模糊效果就会消失 self.navigationController.navigationBar.barTintColor = [UIColor purpleColor]; //导航栏的透明度(模糊效果) /* 模糊效果打开:视图的坐标原点是屏幕的左上角 模糊效果关闭:视图的坐标原点是导航栏的左下角 导航栏高度(包含状态栏)64 状态栏高度是 20 */ self.navigationController.navigationBar.translucent = NO ; //关闭透明度 (默认是打开的) /* //导航栏的背景图片 //高度64 包括状态栏的高度 (图片320 * 64) [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_64.png"] forBarMetrics:UIBarMetricsDefault]; // 44 不包括状态栏 (图片320 * 44) [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_44.png"] forBarMetrics:UIBarMetricsDefault]; // 30 平铺 铺满导航栏和状态栏 (图片320 * 30) [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_30.png"] forBarMetrics:UIBarMetricsDefault]; */ //self.navigationItem 每个页面显示在导航栏上的信息 //系统自带的样式· /* UIBarButtonSystemItemDone, UIBarButtonSystemItemCancel, UIBarButtonSystemItemEdit, UIBarButtonSystemItemSave, UIBarButtonSystemItemAdd, UIBarButtonSystemItemFlexibleSpace, UIBarButtonSystemItemFixedSpace, UIBarButtonSystemItemCompose, UIBarButtonSystemItemReply, UIBarButtonSystemItemAction, UIBarButtonSystemItemOrganize, UIBarButtonSystemItemBookmarks, UIBarButtonSystemItemSearch, UIBarButtonSystemItemRefresh, UIBarButtonSystemItemStop, UIBarButtonSystemItemCamera, UIBarButtonSystemItemTrash, UIBarButtonSystemItemPlay, UIBarButtonSystemItemPause, UIBarButtonSystemItemRewind, UIBarButtonSystemItemFastForward, */ self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(itemAction:)]; /* //自定义文字标题的BarButtonItem (单个) self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"点击" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)]; //自定义多个BarButtonItem 加在leftItem (多个) UIBarButtonItem *leftItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(itemAction:)]; // UIBarButtonItem *leftItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(itemAction:)]; // // //同时添加多个BarButtonItem self.navigationItem.leftBarButtonItems = @[leftItem1,leftItem2]; */ /* //自定义左右BarButtonItem UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; rightButton.frame = CGRectMake(0, 0, 28, 28); [rightButton setImage:[UIImage imageNamed:@"iconfont-card-1"] forState:UIControlStateNormal]; [rightButton addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton]; UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; leftButton.frame = CGRectMake(0, 0, 28, 28); [leftButton setImage:[UIImage imageNamed:@"iconfont-card-1"] forState:UIControlStateNormal]; [leftButton addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton]; */ //渲染为原图 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"iconfont-card-1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)]; //中间的titleView 可以自定义一个view,label都可以 self.navigationItem.titleView = [[UISegmentedControl alloc]initWithItems:@[@"控件一",@"控件二"]]; /* //导航栏上的原色颜色 self.navigationController.navigationBar.tintColor = [UIColor redColor]; //返回上一控制器 [self.navigationController popViewControllerAnimated:YES]; //返回某一控制器 [self.navigationController popToViewController:[[UIViewController alloc] init] animated:YES]; //返回到根视图 [self.navigationController popToRootViewControllerAnimated:YES]; //B pop 返回A后,B是否释放要看之后的代码是否再次使用了B,如果后面的代码还使用到了B,则B所占的内存一直保留,有一种情况例外,在内存极度不足的情况下,IOS会遵从一定的策略有可能把B释放,B再次被使用的时候再重新创建另外,即使pop后后面的代码不再使用B了,那么B的内存只是有可能被释放,释放的时机并不一定,这取决于垃圾回收器的回收策略 */ /* navigationController自带了一个工具栏,通过设置 self.navigationController.toolbarHidden = NO来显示工具栏,工具栏中的内容可以通过viewController的toolbarItems来设置,显示的顺序和设置的NSArray中存放的顺序一致,其中每一个数据都一个UIBarButtonItem对象,可以使用系统提供的很多常用风格的对象,也可以根据需求进行自定义。 设置Toolbar内容的例子: */ self.navigationController.toolbarHidden = NO ; UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]; UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil]; UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil]; UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil]; UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]]; } //UINavigationControllerDelegate //相当于 viewController要显示的时候通知一下外面,给你一个机会进行设置,包含如下两个函数: -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { } -(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { } -(void)rightAction:(UIBarButtonItem *)itemButton{ SecondViewController *secongVC= [[SecondViewController alloc] init]; //从当前视图push到目标视图,如果视图已经存在,则不影响堆栈 [self.navigationController pushViewController:secongVC animated:YES]; } -(void)Action:(UIButton *)btn { }
标签:
原文地址:http://www.cnblogs.com/yyxblogs/p/4821092.html