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

更改导航栏的背景和文字Color

方法一:

  //set NavigationBar 背景颜色&title 颜色

  1. [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];
  2. [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];

效果如下:

我们把背景改成了蓝色,title文字改成了白色,是不是很简单呢?NavigationBar极其push过去的子页面也会是你修改后的背景颜色

方法二:

  1. //设置NavigationBar背景颜色
  2. [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
  3. //@{}代表Dictionary
  4. [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

效果如下:

在导航栏使用背景图片:

如果您的应用程序使用了自定义图像作为栏的背景,你需要提供一个“更大”的图片,使其延伸了状态栏的后面。导航栏的高度现在是从44点(88像素)更改为64点(128像素)。
仍然可以使用了setBackgroundImage:方法来指定自定义图像的导航栏。下面是代码行设置背景图片:

  1. [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];

效果图和上面的一样,我就不贴出来了。

改变导航栏标题的字体

就像iOS 6,我们可以通过使用导航栏的“titleTextAttributes”属性来自定义的文本样式。可以指定字体,文字颜色,文字阴影颜色,文字阴影在文本标题偏移属性字典,使用下面的文本属性键:

UITextAttributeFont - 字体
UITextAttributeTextColor - 文字颜色
UITextAttributeTextShadowColor - 文字阴影颜色
UITextAttributeTextShadowOffset - 偏移用于文本阴影

  1. NSShadow *shadow = [[NSShadow alloc] init];
  2. shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
  3. shadow.shadowOffset = CGSizeMake(0, 1);
  4. [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
  5. [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
  6. shadow, NSShadowAttributeName,
  7. [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil nil]];

使用图片作为导航栏标题

不想标题栏是光秃秃的文字?可以通过使用代码行中的图像或标志取代它:简单地改变titleview用来自定义,(适用于较低版本)

  1. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appcoda-logo.png"]];

效果如下,我随便用了个图片,别介意:

添加多个栏按钮项目

您希望添加导航栏的一侧不止一个栏按钮项目,无论是leftBarButtonItems和rightBarButtonItems 您在导航栏左侧/右侧指定自定义栏按钮项目。比如你想添加一个摄像头和一个共享按钮右侧的吧。您可以使用下面的代码:

  1. UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action: nil nil];
  2. UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action: nil nil];
  3. NSArray *itemsArr = @[shareItem,cameraItem];
  4. self.navigationItem.rightBarButtonItems = itemsArr;

自定义后退按钮的文字和颜色

通常情况下,我们使用UINavigationController时,push到的子页面,左上角会是系统自动取值上一层父页面的title名称,默认情况是这样,那么我们该如何修改它呢?

左侧显示了父页面的title:用户登录,可是我们想修改成返回,方式有很多,举些例子

方法一:

通过设置navigationItem的backBarButtonItem可以直接更换文字,【注意,要在父视图的Controller中设置】如下:

  1. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
  2. self.navigationItem.backBarButtonItem = item;

效果如下:

所有的子界面返回时都变成了我们定义的文字,如果不想显示文字,直接"",就会单独显示一个系统的返回箭头图标,也是很清晰的感觉。

做到这里发现文字颜色和背景有重复,那么如何自定义其颜色呢?在iOS7,可以改变tintColor属性,它提供了一个快速和简单的方式,下面是一个示例代码片段:

  1. [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

效果如下:

全是系统的图标和文字,这回看着舒服了,有木有?【除了后退按钮,请注意,tintColor属性影响所有按钮标题和按钮图像】

最后举个例子,另外一种实现自定义导航控制器返回按钮,代码如下:

  1. [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];
  2. self.title=[NSString stringWithFormat:@"第%lu页",(unsigned long)self.navigationController.viewControllers.count];
  3. //自定义返回按钮
  4. UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
  5. [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  6. //将返回按钮的文字position设置不在屏幕上显示
  7. [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];

效果如下:

最后说一下使用pushViewController切换到下一个视图时,navigation controller按照以下3条顺序更改导航栏的左侧按钮(本段摘自网络):

1、如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;
2、如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;
3、如果前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题;

iOS导航栏背景,标题和返回按钮文字颜色的更多相关文章

  1. 自定义iOS7导航栏背景,标题和返回按钮文字颜色

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

  2. 【转】自定义iOS7导航栏背景,标题和返回按钮文字颜色 -- 不错不错!!

    原文网址:http://blog.csdn.net/mad1989/article/details/41516743 在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更 ...

  3. 【转】 自定义iOS7导航栏背景,标题和返回按钮文字颜色

    原文:http://blog.csdn.net/mad1989/article/details/41516743 UIBarButtonItem,navigationItem,backBarButto ...

  4. ios 导航栏 点击barbutton的按钮 下拉列表

    环境:xocde5.0.2+ios7.0.1 1.导航栏 ----点击科目--------下拉列表 代码:NGRightTableViewViewController.h #import <UI ...

  5. IOS修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色

    自从IOS7后UINavigationBar的一些属性的行为发生了变化.你可以在下图看到: 现在,如果你要修改它们的颜色,用下面的代码: 1 2 3 4 self.navigationControll ...

  6. 导航栏使用UIButton自定义返回按钮的图片

    - (void)viewDidLoad { UIButton *backItem = [UIButton buttonWithType:UIButtonTypeCustom]; UIBarButton ...

  7. 微信小程序 自定义头部导航栏和导航栏背景图片 navigationStyle

    ​ 这两天因为要做一个带背景的小程序头,哭了,小程序导航栏有背景也就算了,还得让导航栏上的背景顺下来,心态小崩.现在可以单独设置一个页面的小程序头了,但是前提是要微信7.0以上的版本,考虑到兼容性问题 ...

  8. iOS 设置导航栏 返回按钮文字隐藏

    //隐藏返回按钮文字 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) f ...

  9. Swift - 修改导航栏“返回”按钮文字,图标

    Swift - 修改导航栏“返回”按钮文字,图标 2015-11-27 09:13发布:hangge浏览:4037   项目中常常会使用 UINavigationController 对各个页面进行导 ...

随机推荐

  1. python之函数用法all

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法all #all(iterable) #说明:如果iterable的所有元素不为0.' ...

  2. 社区类 App 如何引导用户发帖和产生内容?

    作者:Pmer在路上链接:http://www.zhihu.com/question/25502904/answer/31342246来源:知乎著作权归作者所有,转载请联系作者获得授权. ugc的产出 ...

  3. IDEA+SpringMVC+Spring+Mybatis

    详细参照: SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 这里只说一下注意的地方: 1.上面那篇是用的eclipse, 但IDEA的目录结构和eclipse稍有不同. ...

  4. Everything:速度最快的文件名搜索工具

    http://xbeta.info/everything-search-tool.htm Everything(官网|中文主页|教程)是速度最快的文件名搜索软件.其速度之快令人震惊,百G硬盘几十万个文 ...

  5. python学习笔记——

    python线程的GIL GIL (全局解释器锁)python --- > 支持多线程 ----> 同步和互斥 --->加锁 --->解释器加锁 ————> 解释器同一时 ...

  6. 未能找到类型集或命名空间名称 "xxxxxx" (是否缺少using 指令或引用?)

    “未能找到类型或命名空间名称XXXX”,以往遇到这种情况第一时间想到就是没有引用需要的dll. 但今天我反复检查了好几次,还是没有解决问题.我注意到除了错误信息,还有几个警告信息“未能解析引用的程序集 ...

  7. java 随机日期

    java 在某个范围日期内获取一个日期,再以这个日期作为开始日期,获取到随机n天后的日期 /** * 在beginDate和endDate之间获取一个随机日期作为开始日期 * @param begin ...

  8. 修改tmp的临时目录的地址

    https://nkcoder.github.io/2014/04/11/jetty-as-linux-service/ 我们在启动服务的时候添加-Djava.io.tmpdir=/data/jett ...

  9. [hihoCoder] #1044 : 状态压缩·一

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在兑换到了喜欢的奖品之后,便继续起了他们的美国之行,思来想去,他们决定乘坐火车前往下一座城市——那座城市即将 ...

  10. Java 并发

    参考:http://www.cnblogs.com/dolphin0520/category/602384.html