APPDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

//创建主界面,导航栏的第一个页面

FirstViewController *fvc = [[FirstViewController alloc]init];

//创建一个导航栏控制器

UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:fvc];

nav.navigationBar.barTintColor = [UIColor redColor];

self.window.rootViewController = nav;//设置根控制器

[self.window makeKeyAndVisible];

return YES;

}

//给导航栏添加左右按钮

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"cancel" style:UIBarButtonItemStyleDone target:nil action:nil];

self.navigationItem.leftBarButtonItem = cancelButton;

self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]initWithTitle:@"next" style:UIBarButtonItemStyleDone target:self action:@selector(goToSecond)];

self.navigationItem.rightBarButtonItem = nextButton ;

self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

UILabel *yLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];

yLabel.backgroundColor = [UIColor yellowColor];

yLabel.text = @"新浪";

yLabel.font = [UIFont systemFontOfSize:22];

yLabel.textColor = [UIColor redColor];

yLabel.textAlignment = NSTextAlignmentCenter;

self.navigationItem.titleView = yLabel;

-(void)goToSecond{

//创建即将显示的界面

SecondViewController *svc = [[SecondViewController alloc]init];

//使用导航栏控制器切换页面

//    [self.navigationController presentViewController:svc animated:YES completion:nil];

//push入栈 将当前的界面入栈,这个界面消失,将新的界面推送出来

//pop 将当前的界面消失,从栈上最上面的界面(最后一个界面)出栈

//设置默认提供的返回按钮的标题

UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];

self.navigationItem.backBarButtonItem = backItem;

[self.navigationController showViewController:svc sender:self];

}

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

//显示工具栏

self.navigationController.toolbarHidden = NO;

self.navigationController.toolbar.barTintColor = [UIColor greenColor];

//默认工具栏高44,导航栏高44

//可以隐藏导航栏

//    self.navigationController.navigationBarHidden = YES;

}

SecondViewController类.m

-(void)viewDidLoad{

[super viewDidLoad];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btn setTitle:@"back" forState:UIControlStateNormal];

btn.frame = CGRectMake(100, 200, 120, 50);

[btn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];

btn.tintColor = [UIColor whiteColor];

[self.view addSubview:btn];

}

-(void)goback{

//present 在现有的界面上盖上一层,dismissViewController删除

//如果之前使用 的push 那么用pop

[self.navigationController popViewControllerAnimated:YES];

[self dismissViewControllerAnimated:YES completion:nil];

}

拓:navigationbar 上加多个button:

UIView *rightBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 31)];UIButton *phonebutton = [UIButton buttonWithType:UIButtonTypeCustom];phonebutton.frame=CGRectMake(0, 5, 25, 25);[phonebutton setImage:[UIImage imageNamed:@"phone.png"] forState:UIControlStateNormal];[phonebutton addTarget:self action:@selector(phoneclick)forControlEvents:UIControlEventTouchDown];[rightBarView addSubview:phonebutton];UIButton *mapbutton = [UIButton buttonWithType:UIButtonTypeCustom];[mapbutton setFrame:CGRectMake(30, 5, 25, 25)];[mapbutton setImage:[UIImage imageNamed:@"c_address.png"] forState:UIControlStateNormal];[mapbutton addTarget:self action:@selector(mapclick)forControlEvents:UIControlEventTouchDown];[rightBarView addSubview:phonebutton];[rightBarView addSubview:mapbutton];rightBarView.backgroundColor=[UIColor clearColor];UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBarView];self.navigationItem.rightBarButtonItem = rightBtn;

更简便的方法:

转载于:https://www.cnblogs.com/yangqinglong/p/5363708.html

UIBarbuttonItem相关推荐

  1. UIBarButtonItem使用困惑

    之前大家是否疑惑为什么设置了类似这样的代码 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"返回 ...

  2. IOS开发中UIBarButtonItem上按钮切换或隐藏实现案例

    IOS开发中UIBarButtonItem上按钮切换或隐藏案例实现案例是本文要介绍的内容,这个代码例子的背景是:导航条右侧有个 edit button,左侧是 back button 和 add bu ...

  3. IOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    转:http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINaviga ...

  4. UINavigationBar UINavigationItem UIBarButtonItem

    2019独角兽企业重金招聘Python工程师标准>>> UINavigationBar 主要用来设置导航栏的背景图片 UINavigationItem 主要是作为UIBarButto ...

  5. UINavigationItem 设置UIBarButtonItem

    转:http://hi.baidu.com/ivan_xu/item/237bb1ad77eff9b028ce9d7c 有A.B两个ViewController,假如A push B: UINavig ...

  6. UIToolBar,UIBarButtonItem

    1.工具栏UIToolBar管理了一组UIBarButtonItem, UIBarButtonItem不能随意摆放在屏幕上,它不是继承自UIView.它由导航栏,标签栏或工具栏管理. 栏按钮条目UIB ...

  7. 18.自定义键盘 toolbar的barTintColor UIBarButtonItem的使用 datePick使用

    UIDatePicker *dateP=[[UIDatePicker alloc]init]; dateP.datePickerMode=UIDatePickerModeDate; //设置语言为中文 ...

  8. iOS11 自定义UIBarButtonItem显示异常解决方案

    使用button加载网络图片显示异常 iOS11之前正常: self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];[self.l ...

  9. UIBarButtonItem导航条按钮封装

    系统导航条应该再iOS开发项目中都会有用到.但是有时间长篇大论的在C里边写一大串代码,未必会有些繁杂.基于封装角度,今天我们来做一个导航条UIBarButtonItem的进一步封装,让代码更简洁 今天 ...

最新文章

  1. Flutter开发之HTTP网络请求:dio库(28)
  2. cc2530定时器和捕获比较_STM32学习日志——输入捕获实验(20.06.26)
  3. Java反编译工具-JD-GUI解决直接打开.class文件卡顿问题
  4. Binder源码分析之ServiceManager(原)
  5. 关于框架的胡言乱语(上)
  6. eclipse软件有时会退出弹出一串错误弹框_修复iPhone上的iOS 13软件更新失败错误...
  7. VS2010附加进程调试DLL时断点无法断下的解决方法
  8. 苹果概念手机_吊打华为苹果!马云概念手机终于问世?
  9. axure 元件_在Axure中实现波纹点击特效按钮的方法
  10. [CTO札记]第1天:认识人、熟悉环境
  11. jQuery数据与Attr?
  12. linux下syslog命令,syslog命令
  13. NoiseAsh Rule Tec All Collection for Mac - 无源均衡器
  14. ubuntu最好用的划词翻译词典:有道词典和GoldenDict
  15. matlab餅秶滲杅芞砓,通信猫
  16. 定时器+echarts运行时间太长导致内存溢出页面崩溃
  17. mistake for/mix up/narrow down等动词词组
  18. 集合论的ZFC公理系统
  19. java实现日历打印项目
  20. PV140R1K1T1NMR1派克柱塞泵

热门文章

  1. 【牛客 - 303K第十五届浙江大学宁波理工学院程序设计大赛(同步赛)】Technology Tree(树形dp,tricks)
  2. C 的Pair用法分类整理(精)
  3. 【51Nod - 1001 】 数组中和等于K的数对 (排序+ 尺取)
  4. IMX6怎么移植最新Android,[IMX6Q][Android5.1]移植筆記 --- 無法掛載system文件系統
  5. php实现播放直播_PHP直播技术分享(一:实现直播)
  6. java中白盒测试用例_基于JAVA开发的中国象棋游戏的开发与研究白盒测试用例.doc...
  7. idea 升级到2020后 无法启动_升级iOS 14尝鲜后,无法降级 iOS13.5.1?
  8. python 可视化大屏幕_如何用python搭建可视化看板?
  9. html table vtop,PART2HTML语言.doc
  10. leetcode179. 最大数