重载 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 对各个页面进行导 ...
随机推荐
- java8 - 3
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.functio ...
- PHP将对象转换成数组的方法(兼容多维数组类型)
/** * @author gayayang * @date 2012-8-21 * @todo 将对象转换成数组 * @param unknown_type $obj * @return unkno ...
- linux 101 hacks 3null 改文件大小写 xargs
禁止标准输出和错误信息的输出 当我们调试 shell 脚本的时候,我们往往不希望看到标准输出和标准错误的信息.我们可以使用/dev/nulll 来禁止标准错误的信息. 将标准输出重定向到/dev/nu ...
- MVC设计模式一
一:基础知识 1.mvc model view control 2.模型 是应用程序的主体部分,模型表示业务数据与业务逻辑. 一个模型可以为多个视图提供数据 提高了代码的可重用性 3.视图 用户看到的 ...
- JS日期、时间 格式化转换方法
Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+& ...
- FPGA+ARM or FPGA+DSP?
网上有人说.现在的FPGA,ARM功能已经强大到无需DSP协助处理了,未来DSP会不会消声灭迹?是DSP取代FPGA和ARM,还是ARM,FPGA取代DSP呢?担心好不容易学精了DSP,结果DSP变成 ...
- HDU - 1754 A - I Hate It 线段树
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Python处理海量数据的实战研究
最近看了July的一些关于Java处理海量数据的问题研究,深有感触,链接:http://blog.csdn.net/v_july_v/article/details/6685962 感谢July ^_ ...
- python opencv3 窗口显示摄像头的帧
git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 在窗口显示摄像 ...
- luoguP3235 [HNOI2014]江南乐 数论分块 + 博弈论
感觉其实很水? 题目就是一个Multi SG游戏,只需要预处理出所有的\(sg\)值即可\(O(Tn)\)计算 对于计算\(sg[n]\)而言,显然我们可以枚举划分了\(x\)堆来查看后继状态 那么, ...