重载 UINavigationController 设置左侧返回按钮的文字为图片

UINavigationController
导航栏控制器的左侧返回按钮如果需要设置成图片,仅使用系统的是无法实现的,需要重载系统的导航栏控制器,在控制器推出之前替换掉leftBarButtonItem才行.
注:以下链接的这个哥们对NavigationViewController所有能做的定制都解说了
http://beyondvincent.com/blog/2013/11/03/120-customize-navigation-status-bar-ios-7/#5
源码如下:
CustomNavigationViewController.h + CustomNavigationViewController.m
#import <UIKit/UIKit.h> @interface CustomNavigationViewController : UINavigationController @end
#import "CustomNavigationViewController.h" #pragma mark - 支持ARC与非ARC
#if __has_feature(objc_arc)
#define RELEASE(obj)
#define AUTO_RELEASE(obj)
#else
#define Release(obj) [obj release]
#define Autorelease(obj) [obj autorelease]
#endif //判断是否是iOS7
#define iOS7 \
([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) @interface CustomNavigationViewController () @end @implementation CustomNavigationViewController #pragma mark - 重载父类进行改写
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//先进入子Controller
[super pushViewController:viewController animated:animated]; //替换掉leftBarButtonItem
if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > ) {
viewController.navigationItem.leftBarButtonItem =[self customLeftBackButton];
}
} #pragma mark - 自定义返回按钮图片
-(UIBarButtonItem*)customLeftBackButton{ UIImage *image = [UIImage imageNamed:@"back.png"]; UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(, , image.size.width, image.size.height); [backButton setBackgroundImage:image
forState:UIControlStateNormal]; [backButton addTarget:self
action:@selector(popself)
forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
Autorelease(backItem); return backItem;
} #pragma mark - 返回按钮事件(pop)
-(void)popself
{
[self popViewControllerAnimated:YES];
} #pragma mark - 用图片设置导航背景
+ (void)initialize
{
//取出设置主题的对象
UINavigationBar *navBar = [UINavigationBar appearance]; //设置导航栏的背景图片
NSString *navBarBg = nil;
if (iOS7)
{
navBarBg = @"NavBar64";
navBar.tintColor = [UIColor whiteColor];
}
else
{
navBarBg = @"NavBar";
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
[navBar setBackgroundImage:[UIImage imageNamed:navBarBg] forBarMetrics:UIBarMetricsDefault]; //标题颜色
[navBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
} @end


重载 UINavigationController 设置左侧返回按钮的文字为图片的更多相关文章
- 如何修改 UINavigationController、UINavigationBar 中 navigationItem 左侧 “返回” 按钮的名称
如果我们从 title 为 “首页” 的页面 A 点击进入一个子页面 B,那么在页面 B 的左上角将显示一个名为 “<首页” 的按钮.假设这个页面 A 叫 “你是我天边最美的云彩”,那在页面 B ...
- UINavigationController 返回按钮去掉文字
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetric ...
- iOS 11 使用方法替换(Method Swizzling),去掉导航栏返回按钮的文字
方法一:设置BarButtonItem的文本样式为透明颜色,代码如下: [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegro ...
- iOS不得姐项目--appearance的妙用,再一次设置导航栏返回按钮,导航栏左右按钮的封装(巧用分类)
一.UI_APPEARANCE_SELECTOR 彩票项目中appearance的用法一直没有搞明白,这次通过第二个项目中老师的讲解,更深一层次的了解到了很多关于appearance的作用以及使用方法 ...
- 自定义iOS7导航栏背景,标题和返回按钮文字颜色
在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Col ...
- 【转】自定义iOS7导航栏背景,标题和返回按钮文字颜色 -- 不错不错!!
原文网址:http://blog.csdn.net/mad1989/article/details/41516743 在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更 ...
- 【转】 自定义iOS7导航栏背景,标题和返回按钮文字颜色
原文:http://blog.csdn.net/mad1989/article/details/41516743 UIBarButtonItem,navigationItem,backBarButto ...
- iOS导航栏背景,标题和返回按钮文字颜色
在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Col ...
- Swift - 修改导航栏“返回”按钮文字,图标
Swift - 修改导航栏“返回”按钮文字,图标 2015-11-27 09:13发布:hangge浏览:4037 项目中常常会使用 UINavigationController 对各个页面进行导 ...
随机推荐
- ZooKeeper实践:(2)配置管理
一. 前言 配置是每个程序不可或缺的一部分,配置有多重方式:xml.ini.property.database等等,从最初的单机环境到现在的分布式环境. 1. 以文件的格式存储配置,修改任何都 ...
- Web前端开发最佳实践(10):JavaScript代码不好读,不好维护?你需要改变写代码的习惯
前言 这篇文章本应该在上一篇文章:使用更严格的JavaScript编码方式,提高代码质量之前发布,但当时觉得这篇文章太过基础,也就作罢.后来咨询了一些初级的开发者,他们觉得有必要把这篇文章也放上来.尽 ...
- Django: ImportError: No module named 'corsheaders'
pip3 install django-cors-headers #设置PIP镜像 pip3 --trusted-host pypi.python.org install django-cors-he ...
- iconfont 在项目中的简单使用
font-class引用 font-class是unicode使用方式的一种变种,主要是解决unicode书写不直观,语意不明确的问题. 与unicode使用方式相比,具有如下特点: 兼容性良好,支持 ...
- Linux下安装Zookeeper
Zookeeper是一个协调服务,可以用它来作为配置维护.名字服务.分布式部署: 下面,我来分享一下在Linux下安装Zookeeper的整个步骤,让大家少走弯路. 一.Zookeeper下载 [ro ...
- MyISAM InnoDB 区别(转载)
MyISAM 和 InnoDB 讲解 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处 ...
- java面试数据类型
1. Java的数据类型? 2. Java的封装类型? 3. 基本类型和封装类型的区别? 基本类型只能按值传递,而对应的封装类是按引用传递的. 基本类型是在堆栈上创建的,而所有的对象类型都是在堆上创建 ...
- TCP流嗅探和连接跟踪工具tcpick
TCP流嗅探和连接跟踪工具tcpick 由于网络通信协议众多,TCP连接状态众多,所以TCP分析较为复杂.Kali Linux提供一款专用工具tcpick.该工具支持在线实时嗅探和离线文件嗅探.它 ...
- Winform 串口通讯之读卡器
老板给我的第一个硬件就是一个读卡器, 说让我做一下试试,于是从网上查了查就写了出来,相当的简单. 但是后来还有一个地磅的串口通讯,我整整搞了一天. 在窗体类的构造函数中写入 Form.CheckFor ...
- Linux下gcc与g++用法以及编写makefile
1. gcc与g++编译流程: 1) 编译流程: 2) 预处理:生成.i的预处理文件. Ø 只激活预处理,这个不生成文件,需要把它重定向一个输出文件. ...