【iOS开发-22】navigationBar导航栏,navigationItem建立:获取导航栏中的基本文本和button以及各种跳跃
(1)navigationBar导航栏可以被看作是self.navigationController一个属性导航控制器,它可以由点直接表示self.navigationController.navigationBar。当然navigationBar他还是很物业。让我们风格barStyle、背景backgroundColor、frame属性(能够获取宽高这些信息)。还能够用setBackgroundImage方法设置背景图片。当然图片多了能够使用clipsToBounds剪裁。
(2)但。navigationBar是否隐藏和显示这个须要它爸也就是self.navigationController来控制,有直接.navigationBarHidden设置为YES/NO,也能够用方法setNavigationBarHidden,都能实现效果。
(3)还有一个重要的知识是对navigationItem的设置,这个属性和navigationController是平级的,所以直接能够用self.navigationItem使用。当然可用的有设置导航条标题的方法setTitle,当然你也能够直接把文字换成一个视图。即所谓的标题视图放在导航条的中间,用得方法是setTitleView,非常多游戏的导航条中间貌似是一个图片,能够用这个。
(4)最重要的可能是给navigationItem设置左右两边的button,一般默认的在左边有“返回”。在右边的有“摄像头”(如微信朋友圈)。步骤就是创建一个UIBarButtonItem对象,然后直接把这个对象赋值给self.navigationItem.leftBarButtonItem或者右边的。当然也能够一次创建非常多个UIBarButtonItem组成一个数组。然后把这个数组赋值给self.navigationItem.leftBarButtonItems。注意后面这个和前面这个相比,多了一个“s”。有非常多个。也要注意一下有多个button时的排列顺序。
(5)我们创建的这些导航条button有非常多种形式。有的是由文字的,有的时图片,有的时系统自带的如摄像头或者Reply这些icon,有的全然是自定义的视图。
我们当然也能够利用自己创建的导航条button来覆盖原来导航控制器产生的默认的button,如“<Back”。
相同。须要创建两个视图控制器(ViewController根视图控制器,SecondViewController子视图控制器),然后放在导航控制器栈中。而且在AppDelegate.m中进行把导航控制器赋值给self.window.rootViewController。
在ViewController.m中:
#import "ViewController.h"
#import "SecondViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
//创建一个button,点击后进入子视图控制器,相当于进入子页面
UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame=CGRectMake(38, 100, 300, 30);
[btn1 setTitle:@"jump to secondviewcontroller" forState:UIControlStateNormal];
btn1.backgroundColor=[UIColor whiteColor];
self.view.backgroundColor=[UIColor redColor];
[btn1 addTarget:self action:@selector(jumpTo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
//设置导航条样式
//默认的时白色半透明(有点灰的感觉),UIBarStyleBlack,UIBarStyleBlackTranslucent,UIBarStyleBlackOpaque都是黑色半透明。事实上它们有的时不透明有的时透明有的时半透明,但不知为何无效果
self.navigationController.navigationBar.barStyle=UIBarStyleDefault;
//设置导航条背景颜色,也是半透明玻璃状的颜色效果
self.navigationController.navigationBar.backgroundColor=[UIColor orangeColor];
//能够用self.navigationController.navigationBar.frame.size获得高宽,还有self.navigationController.navigationBar.frame.origin获得x和y
//高44。宽375,假设是Retina屏幕,那么宽和高@2x就可以各自是750和88
//x是0非常明显,y是20。当中上面20就是留给状态栏的高度
NSLog(@"%f",self.navigationController.navigationBar.frame.origin.y); //隐藏导航条,由此点击进入其它视图时导航条也会被隐藏。默认是NO
//以下一个直接给navigationBarHidden赋值,一个调用方法,都是一样的,以下一个多了一个动画选项而已
self.navigationController.navigationBarHidden=NO;
[self.navigationController setNavigationBarHidden:NO animated:YES];
//给导航条添加背景图片,当中forBarMetrics有点相似于button的for state状态,即什么状态下显示
//UIBarMetricsDefault-竖屏横屏都有。横屏导航条变宽。则自己主动repeat图片
//UIBarMetricsCompact-竖屏没有,横屏有,相当于之前老iOS版本号里地UIBarMetricsLandscapePhone
//UIBarMetricsCompactPrompt和UIBarMetricsDefaultPrompt临时不知道用处。官方解释是Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar,以后遇到时再细说
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"big2.png"] forBarMetrics:UIBarMetricsDefault];
//假设图片太大会向上扩展侵占状态栏的位置,在状态栏下方显示
//clipsToBounds就是把多余的图片裁剪掉
self.navigationController.navigationBar.clipsToBounds=YES; //设置导航标题
[self.navigationItem setTitle:@"主页"]; //设置导航标题视图,就是这一块能够载入随意一种视图
//视图的x和y无效。视图上下左右居中显示在标题的位置
UIView *textView1=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 50, 30)];
textView1.backgroundColor=[UIColor whiteColor];
[self.navigationItem setTitleView:textView1]; //设置导航条的左右button
//先实例化创建一个UIBarButtonItem,然后把这个button赋值给self.navigationItem.leftBarButtonItem就可以
//初始化文字的button类型有UIBarButtonItemStylePlain和UIBarButtonItemStyleDone两种类型,差别貌似不大
UIBarButtonItem *barBtn1=[[UIBarButtonItem alloc]initWithTitle:@"左边" style:UIBarButtonItemStylePlain target:self action:@selector(changeColor)];
self.navigationItem.leftBarButtonItem=barBtn1; //我们还能够在左边和右边加不止一个button。,且能够加入随意视图,以右边为例
//加入多个事实上就是rightBarButtonItems属性,注意另一个rightBarButtonItem,前者是赋予一个UIBarButtonItem对象数组。所以能够显示多个。后者被赋值一个UIBarButtonItem对象,所以仅仅能显示一个
//显示顺序,左边:按数组顺序从左向右;右边:按数组顺序从右向左
//能够初始化成系统自带的一些barButton,比方UIBarButtonSystemItemCamera是摄像机,还有Done。Reply等等,会显示成一个icon图标
//还能够initWithImage初始化成图片
//还能够自己定义。能够是随意一个UIView
UIBarButtonItem *barBtn2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(changeColor2)];
UIBarButtonItem *barBtn3=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"logo-40@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(changeColor3)];
UIView *view4=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
view4.backgroundColor=[UIColor blackColor];
UIBarButtonItem *barBtn4=[[UIBarButtonItem alloc]initWithCustomView:view4];
NSArray *arr1=[[NSArray alloc]initWithObjects:barBtn2,barBtn3,barBtn4, nil];
self.navigationItem.rightBarButtonItems=arr1;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} -(void)changeColor{
self.view.backgroundColor=[UIColor purpleColor];
}
-(void)changeColor2{
self.view.backgroundColor=[UIColor whiteColor];
}
-(void)changeColor3{
self.view.backgroundColor=[UIColor orangeColor];
} -(void)jumpTo{
//这里面核心的有两个,所谓跳转,事实上就是往导航控制器栈中PUSH或者POP一个视图控制器,这样在最上面的视图控制器就变了,这样视图也跟着变了,由于仅仅显示在栈顶得那个视图控制器的视图
//所以(1)控制所谓的跳转。事实上是导航控制器在控制,在里面的元素都能够通过navigationController属性获取到它们所在的导航控制器
//所以(2)获取到导航控制器之后,使用Push的那个方法,往栈里面放一个视图控制器senCon1,这个新放入的在栈顶。就显示它的视图,所以用户改变页面跳转了
SecondViewController *senCon1=[[SecondViewController alloc]init];
[self.navigationController pushViewController:senCon1 animated:YES];
} @end
在SecondViewControllor.m中:
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
UILabel *label1=[[UILabel alloc]init];
label1.frame=CGRectMake(38, 80, 300, 30);
label1.backgroundColor=[UIColor whiteColor];
label1.text=@"This is secondviewcontroller";
[self.view addSubview:label1];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame=CGRectMake(38, 120, 300, 30);
[btn2 setTitle:@"backTo" forState:UIControlStateNormal];
btn2.backgroundColor=[UIColor orangeColor];
[self.view addSubview:btn2];
[btn2 addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
//设置导航标题,这个时候的返回button的title就是上一级的navigationItem的title文字
[self.navigationItem setTitle:@"子页"];
//我们也能够在子页中自己定义一个返回button覆盖原先的"<back"
UIBarButtonItem *barBtn5=[[UIBarButtonItem alloc]initWithTitle:@"回家" style:UIBarButtonItemStylePlain target:self action:@selector(backTo)];
self.navigationItem.leftBarButtonItem=barBtn5;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)backTo{
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
@end
截个图:
【iOS开发-22】navigationBar导航栏,navigationItem建立:获取导航栏中的基本文本和button以及各种跳跃的更多相关文章
- 【iOS开发-22】navigationBar导航条和navigationItem设置:基本搞定导航条上的文字和按钮以及各种跳转
http://blog.csdn.net/weisubao/article/details/39646739?utm_source=tuicool&utm_medium=referral (1 ...
- iOS开发之线程间的MachPort通信与子线程中的Notification转发
如题,今天的博客我们就来记录一下iOS开发中使用MachPort来实现线程间的通信,然后使用该知识点来转发子线程中所发出的Notification.简单的说,MachPort的工作方式其实是将NSMa ...
- ios开发总结,日常开发:ios开发功能收集,经验分享等等(不断更新中。。。)
github资料学习和下载地址:https://github.com/niexiaobo/MyDailyDevelopmentNotes ios 学习模块 ios APP 日志管理的重要性: 一个功能 ...
- iOS开发——数据持久化Swift篇&文件目录路径获取(Home目录,文档目录,缓存目录等)
文件目录路径获取(Home目录,文档目录,缓存目录等) iOS应用程序只能在自己的目录下进行文件的操作,不可以访问其他的存储空间,此区域被称为沙盒.下面介绍常用的程序文件夹目录: 1,Home ...
- iOS开发手记 - iOS9.3 UINavigationController添加后不显示storyboard中viewcontroller里的控件的解决方法
我原先是这么做的,通常也是这么做 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSD ...
- IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)
编写一个如下界面,实现: 1.在文本输入框中输入一个网址,然后点击显示图片,图片显示到UIImageView中. 2.点击下载,这张显示的图片被下载到手机的Documents文件夹下的Dowmload ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- iOS开发UINavigation——导航控制器UINavigationController
iOS开发UINavigation系列一——导航栏UINavigtionBar摘要iOS中的导航条可以附着于导航控制器之中使用,也可以在controller中单独使用,这篇博客,主要讨论有关导航栏的使 ...
- iOS开发——高级篇——地图 MapKit
一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如周边:找餐馆.找KTV.找电影院等等导航:根据用户设定的起点和终点,进行路线规划,并指引用户如何到达 在上述应用中,都用到了定位 ...
随机推荐
- 正則表達式验证邮箱,qq,座机,手机,网址
手机: var reg=/^1[34578]\d{9}$/; if(reg.test("你输入的手机号码") ) { alert("手机号码输入正确") } e ...
- UNICODE和ANSI字符串的转换(解释了MultiByteToWideChar,WideCharToMultiByte,GetTextCharsetInfo,GetTextCharset,IsDBCSLeadByte,IsDBCSLeadByteEx,IsTextUnicode一共7个函数)
继上集故事<多字符集(ANSI)和UNICODE及字符串处理方式准则 >,我们现在有一些特殊需求: 有时候我们的字符串是多字符型,我们却需要使用宽字符型:有的时候却恰恰相反. Window ...
- 14.5.1 Resizing the InnoDB System Tablespace
14.5.1 Resizing the InnoDB System Tablespace 本节描述如何增加或者减少InnoDB 系统表空间的大小 增加InnoDB 系统表空间的大小 最简单的方式增加I ...
- NginX issues HTTP 499 error after 60 seconds despite config. (PHP and AWS)
FROM: http://stackoverflow.com/questions/15613452/nginx-issues-http-499-error-after-60-seconds-despi ...
- Delphi基础Write写入结构体到文件(使用 file of myrecord就行了,真简单)
program WriteStruct; {$APPTYPE CONSOLE} uses SysUtils; //写入结构体 type TCustomer = record ID: ]; Code: ...
- LCA 最近公共祖先 tarjan离线 总结 结合3个例题
在网上找了一些对tarjan算法解释较好的文章 并加入了自己的理解 LCA(Least Common Ancestor),顾名思义,是指在一棵树中,距离两个点最近的两者的公共节点.也就是说,在两个点通 ...
- VSTO 得到Office文档的选中内容(Word、Excel、PPT、Outlook)
原文:VSTO 得到Office文档的选中内容(Word.Excel.PPT.Outlook) 目的:得到在Word.Excel.PPT.Outlook中选中的一段内容. Word: private ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- cisco san交换机配置
1.配置交换机的管理地址switch(config)# interface mgmt 0 switch(config-if)# ip adress 192.168.100.108 255.255.25 ...
- Eclipse扩展点实践之添加快捷菜单项(Command方式实现)
有两种方式,一种是Action的方式,另一种是Command的方式(这两种方式的区别详见:http://wiki.eclipse.org/FAQ_What_is_the_difference_betw ...