码迷,mamicode.com
首页 > 移动开发 > 详细

iOS导航栏使用

时间:2015-06-08 16:34:32      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

xcode:6.3.2

ios8.3

介绍:在平时项目开发中,UINavigationController的使用非常广泛,很多优秀的APP都采用了导航栏,导航栏对于视图控制方面非常便捷。

先来看一张官方的图

技术分享

1,新建项目Nav

2,添加FirstViewController用于根视图,添加SecondViewController用于根视图跳转到的视图

技术分享

3,在AppDelegate.m文件中,修改代理,使得FirstViewController为根视图,如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    FirstViewController *first = [[FirstViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:first];
    [self.window setRootViewController:nav];
    [self.window makeKeyAndVisible];
    return YES;
}

4,在FirstViewController.m中设置navigationController的相关属性,如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.title = @"首页"; //设置标题
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];//设置navigationbar的颜色
    
    [self.navigationController.navigationBar setTranslucent:YES];//设置navigationbar的半透明
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(cameraStart)];//设置navigationbar左边按钮
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(toSecond)];//设置navigationbar右边按钮
 
    
}

样式如图所示:

技术分享

5,实现toSecond方法push到SecondViewController

- (void)toSecond {
    SecondViewController *second = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:second animated:YES];
}

6,在SecondViewController.m中添加按钮,用于pop回FirstViewController,代码如下

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor blackColor];
    [button addTarget:self action:@selector(backToFirstViewController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
   // [self.navigationController.navigationBar setHidden:YES];
}
- (void)backToFirstViewController {
    [self.navigationController popViewControllerAnimated:YES];
   // [self.navigationController.navigationBar setHidden:NO];

}

样式如下:

技术分享

7,隐藏navigationBar,打开注释,设置navigationBar隐藏,同时在返回FirstViewController的方法中让navigationBar显示回来

[self.navigationController.navigationBar setHidden:YES];

源码地址:https://github.com/rokistar/UsingUINavigationController

iOS导航栏使用

标签:

原文地址:http://www.cnblogs.com/Roki/p/4560954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!