本文是使用纯代码实现一个导航栏的效果。单击按钮并且产生事件。基本思路是:

1.创建一个导航栏(UINavigationBar对象)

2.创建一个导航栏集合(UINavigationItem对象)

3.创建一个左边按钮、一个右边按钮(UIBarButtonItem对象),并实现对应的事件方法

4.将导航栏集合添加到导航栏中,设置动画关闭

5.把左右两个按钮添加到导航栏集合中去

6.在视图中显示当前创建的导航栏

具体的实现代码如下:

ViewController.h文件中的代码不用改变,如下所示:

  1. #import <UIKit/UIKit.h>
  2. @interface ViewController : UIViewController
  3. @end

ViewController.m文件中的代码:

  1. #import "ViewController.h"
  2. @interface ViewController ()
  3. @end
  4. @implementation ViewController
  5. - (void)viewDidLoad
  6. {
  7. [super viewDidLoad];
  8. // Do any additional setup after loading the view, typically from a nib.
  9. //创建一个导航栏
  10. UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  11. //创建一个导航栏集合
  12. UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];
  13. //在这个集合Item中添加标题,按钮
  14. //style:设置按钮的风格,一共有三种选择
  15. //action:@selector:设置按钮的点击事件
  16. //创建一个左边按钮
  17. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonItemStyleBordered target:self action:@selector(clickLeftButton)];
  18. //创建一个右边按钮
  19. UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];
  20. //设置导航栏的内容
  21. [navItem setTitle:@"凌凌漆"];
  22. //把导航栏集合添加到导航栏中,设置动画关闭
  23. [navBar pushNavigationItem:navItem animated:NO];
  24. //把左右两个按钮添加到导航栏集合中去
  25. [navItem setLeftBarButtonItem:leftButton];
  26. [navItem setRightBarButtonItem:rightButton];
  27. //将标题栏中的内容全部添加到主视图当中
  28. [self.view addSubview:navBar];
  29. //最后将控件在内存中释放掉,以避免内存泄露
  30. [navItem release];
  31. [leftButton release];
  32. [rightButton release];
  33. }
  34. -(void)showDialog:(NSString *)str
  35. {
  36. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
  37. [alert show];
  38. [alert release];
  39. }
  40. -(void) clickLeftButton
  41. {
  42. [self showDialog:@"点击了导航栏左边按钮"];
  43. }
  44. -(void) clickRightButton
  45. {
  46. [self showDialog:@"点击了导航栏右边按钮"];
  47. }
  48. - (void)viewDidUnload
  49. {
  50. [super viewDidUnload];
  51. // Release any retained subviews of the main view.
  52. }
  53. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  54. {
  55. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  56. }
  57. @end

IOS导航栏的使用方法的更多相关文章

  1. <iOS 导航栏>第一节:导航栏透明方法实现代码

      说下导航栏的透明方法:   很多应用需要导航栏随着向上滑动,逐渐从透明变成不透明,很炫酷,大部分应用都在使用导航栏渐变效果,现附上代码然后直接将实现,一会讲下如何来实现,这一部分直接上代码.   ...

  2. iOS - 导航栏UINavigationController经常使用属性

    1.设置导航栏标题 self.title = @"dylan_李伟宾"; 2.设置导航栏样式 设置方法: [self.navigationController.navigation ...

  3. 转:ios导航栏设置

    原帖:http://www.cocoachina.com/industry/20131104/7287.html 本文提供的代码需要用Xcode 5来执行.如果你还在使用老版本的Xcode,那么在运行 ...

  4. iOS导航栏背景,标题和返回按钮文字颜色

    在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Col ...

  5. iOS 导航栏黑线,UIImage 枚举处理方式

      ios 找出导航栏下面的黑线(可隐藏,改变样式等) http://www.jianshu.com/p/effa4a48f1e3     设置UIImage的渲染模式:UIImage.renderi ...

  6. ios 导航栏的显示和隐藏切换

    从简单的一个没有导航栏的界面A push到另一个有导航栏的界面 B,在界面A的逻辑中加入下面逻辑: 屏幕快照 2016-03-30 上午10.35.24.png 这样完美的处理了这个场景变换需求.引起 ...

  7. iOS导航栏的正确隐藏方式【转】

    简介:在项目中经常碰到首页顶部是无限轮播,需要靠最上面显示.有的设置导航栏为透明等一系列的方法,这个可以借助第三方.或者干脆简单粗暴的直接隐藏掉导航栏.可是push到下一个页面的时候是需要导航栏的,如 ...

  8. IOS导航栏颜色渐变与常用属性

    (转:http://www.cnblogs.com/Lingchen-start/archive/2015/10/23/4904361.html) 今年很忙,忙的写日志的时间都很少.  少的可怜. 自 ...

  9. iOS 导航栏实现总结

    目标: 在UI界面中实现 整体效果的导航栏, 比如1 首页无导航条,次页有导航条, 2 导航条中不包含下方不包含黑边 3 导航条包含多个筛选项 等等 问题: 用系统带的NavigateBar 来实现时 ...

随机推荐

  1. Webdriver - Selenium Grid Configuration

    Grid parameter: role = <hub|node> (default is no grid, just run an RC/webdriver server). When ...

  2. OneProxy主从延迟检测

    OneProxy具有主从延迟检测功能,检测方式有两种. 1.通过MySQL本身提供的延迟信息,即在从库中执行show slave status; 查看Seconds_Behind_Master值: 2 ...

  3. struts2文件下载相关信息

    struts.xml文件配置: <span style="font-size:16px;"><?xml version="1.0" encod ...

  4. hibernate的懒加载问题

    产生原因: 当使用hibernate查询一个对象的时候,如果Session关闭,再调用该对象关联的集合或者对象的时候,会产生懒加载异常! 解决方案: 方案一: 在Session关闭之前,查询对象关联的 ...

  5. 找不到 -lz解决方法

    sudo apt-get install libghc-zlib-dev

  6. !important css样式

    重要性 我们在做网页代码的时,有些特殊的情况需要为某些样式设置具有最高权值,怎么办?这时候我们可以使用!important来解决. 如下代码: p{color:red!important;} p{co ...

  7. cookie、sessionStorage、localStorage区别

    相同:不管sessionStorage localStorage 还是 cookie 都是存储用户数据的. 不同: 1.cookie的存储空间小, cookie的数据是会通过http请求带到服务器的( ...

  8. 模拟器的tableView的分割线不显示

    只有iOS9和iPhone6 plus模拟器上TableView分割线不会显示. 原因: 由于iPhone6 plus的分辨率较高,开发的时候同常都使用command + 3 或者 command + ...

  9. 使用Astah繪製UML圖形(转)

    http://www.dotblogs.com.tw/clark/archive/2015/02/12/149483.aspx

  10. 石子归并问题(nyoj737)

    石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述     有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的 ...