iOS-个人整理12 - 导航控制器-UINavigationController

一、UINavigationController基本属性

写这么多文章居然没人给我评论几句。。。

UINavigationController,导航控制器,用来管理多个视图控制器
它就是管理视图控制器的控制器
从此可以在Appdelegate.m的launch函数中先声明导航栏控制器,再将它作为视图控制器的爸爸,window的根视图控制器,像下面这样


 //初始化一个表视图控制器  
RootTableViewController *rootTableVC = [[RootTableViewController alloc]init];  
//初始化一个导航控制器,把表视图控制器加上去  
 UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootTableVC];  
 //设置导航控制器为window的根视图控制器  
    self.window.rootViewController = navigationC;  

导航栏有个重要的属性,它的不透明效果,它会影响坐标原点的位置
self.navigationController.navigationBar.translucent
当开启时屏幕左上角为原点
当关闭时,导航栏的左下角为原点
iOS7以后默认为YES

下面说导航控制器的各种属性

    //初始化一个视图控制器  
    RootViewController *rootVC = [[RootViewController alloc]init];  
  
    //初始化导航栏视图控制器,并加载一个根视图控制器  
    UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootVC];  
  
    //设置导航栏视图控制器为根视图控制器  
    self.window.rootViewController = navigationC;  
  
    //标题  
    self.navigationItem.title = @"block首页";  
      
    //设置背景色  
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];  
      
    //标题颜色  
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];  
      
    //导航栏颜色  
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];  
    //导航栏元素颜色  
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];  
  
    //也可以设置中间的标题为视图  
    UISegmentedControl *qqSegmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"消息",@"电话"]];  
      
    self.navigationItem.titleView = qqSegmentedControl;  
  
    //添加左侧按钮,使用系统的按钮样式  
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil]; 
      
    self.navigationItem.leftBarButtonItem = leftBarBtn;  
      
    //添加右侧按钮使用定义文字的初始化方式  
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"去第二页" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];  
      
    self.navigationItem.rightBarButtonItem = rightBarBtn;  
  
    //还可以使用图片给或者View给bar按钮初始化  
    //UIBarButtonItem *test1 = [UIBarButtonItem alloc]initWithImage:<#(nullable UIImage *)#> style:<#(UIBarButtonItemStyle)#> target:<#(nullable id)#> action:<#(nullable SEL)#>  
  
    //UIBarButtonItem *test2 = [UIBarButtonItem alloc]initWithCustomView:<#(nonnull UIView *)#>  
  
    //可以给右侧或者左侧添加按钮组,把按钮放入数组  
    //self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:test,test1, nil];  

系统的导航栏按钮样式有很多,下图很不错,来自友人


二、UINavigationController控制页面跳转

导航栏控制器的主要作用是控制页面跳转
现在我们创建了两个视图控制器:
RootViewController
SecondViewController

1.在AppDelegate.m中创建RootViewController对象和导航控制器

//创建根视图  
   RootViewController *rootVC = [[RootViewController alloc]init];  
   //创建导航控制器  
   UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootVC];  
   //添加到window根视图控制器  
   self.window.rootViewController = navigationC;  

2.在RootViewController.m中给NavigationController添加按钮,给按钮添加跳转方法并实现

- (void)viewDidLoad {  
    [super viewDidLoad];  
      
    //创建右侧按钮  
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"前往第二页" style:UIBarButtonItemStylePlain target:self action:@selector(jumpToSecondVC)];  
      
    //添加按钮到导航栏  
    self.navigationItem.rightBarButtonItem = rightBarBtn;  
}  
-(void)jumpToSecondVC  
{  
    //创建secondVC,要导入SecondViewController的.h文件  
    SecondViewController *secondVC = [[SecondViewController alloc]init];  
      
    //将secondVC入栈,栈顶的视图为正在显示的视图  
    [self.navigationController pushViewController:secondVC animated:YES];  
}  

3.在SecondViewController.m中添加一个返回按钮,点击可以返回rootVC
系统会默认在左上角添加一个返回按钮,我们可以重新添加

- (void)viewDidLoad {  
    [super viewDidLoad];  
    //创建右侧按钮  
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"回到首页" style:UIBarButtonItemStylePlain target:self action:@selector(jumpToRootVC)];  
      
    //添加按钮到导航栏  
    self.navigationItem.rightBarButtonItem = rightBarBtn;  
    // Do any additional setup after loading the view.  
}  
-(void)jumpToRootVC  
{  
    //将当前的视图控制器出栈  
    [self.navigationController popViewControllerAnimated:YES];  
    //也可直接返回根视图控制器  
    [self.navigationController popToRootViewControllerAnimated:YES];  
    //或者返回指定层的视图控制器  
    //可以得到当前栈内的视图,存在数组 self.navigationController.viewControllers里  
    [self.navigationController popToViewController: self.navigationController.viewControllers[0]animated:YES];  
}  

实现了效果也比较简单,点击首页的“前往第二页”可以跳到第二页
点击第二页的“首页”或者“回到首页”可以调回首页,其中“首页”按钮是系统自动加的,“回到首页”是我们自己写的下一节要专门总结一下界面跳转的同时的传值问题

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,663评论 4 61
  • 翻译自“View Controller Programming Guide for iOS”。 1 定义子类 使用...
    lakerszhy阅读 7,385评论 0 5
  • 人生的意义到底是什么? 从生命以个体的形式出现在这个世界,就已踏上一去不返的时光隧道之路,我们经历着相...
    圆夏木阅读 3,805评论 0 6
  • 归家之路从来都没有坦途。 中秋刚过,正值国庆,这一年也是我第二次仓皇回到家。安康到岚皋的路上,我戴着耳塞,托着下巴...
    好名字都取完了阅读 3,921评论 4 6
  • 下午吃饭回来,再次路过楼下那个火锅店,还是一如既往的人满为患,排队的人都轮到三十几号了,其实也是挺佩服那些排队的兄...
    码农另眼看世界阅读 2,361评论 0 0

友情链接更多精彩内容