Jerry Lee

stay hungry,stay young.

Welcome to my world.


运用OC运行时和Method Swizzing来设置返回按钮

在项目开发中,随着界面的增多,为每个控制器自定义返回按钮,每一个控制器中都要写一次,较为麻烦啰嗦。

+ (UIBarButtonItem *)itemWithIcon:(NSString *)icon title:(NSString *)title highLightIcon:(NSString *)highLightIcon target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:highLightIcon] forState:UIControlStateHighlighted];
button.frame = (CGRect){CGPointZero, button.currentBackgroundImage.size};
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:button];
}

然后再每个控制器的viewDidLoad方法中

 self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"backImg.png" title:nil highLightIcon:@"backImgHight.png" target:self action:@selector(TapLeftItemAction) ];

后来无意中看到了这两篇文章(http://wxgbridgeq.github.io),(http://southpeak.github.io/blog/2014/11/06/objective-c-runtime-yun-xing-shi-zhi-si-:method-swizzling/),受到启发.废话少说,直接上代码:

+ (void)load{
swizzleAllViewController();
}
void swizzleAllViewController()
{
Swizzle([UIViewController class], @selector(viewDidAppear:), @selector(customViewDidAppear:));
Swizzle([UIViewController class], @selector(viewWillDisappear:), @selector(customViewWillDisappear:));
Swizzle([UIViewController class], @selector(viewWillAppear:), @selector(customviewWillAppear:));
}
- (void)customviewWillAppear:(BOOL)animated{
if ([[self.navigationController childViewControllers] count] > 1) {
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.backButton];

}
[self customviewWillAppear:animated];
}
- (UIButton *)backButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"btn_back_highLight.png"] forState:UIControlStateHighlighted];
button.frame = (CGRect){CGPointZero, button.currentBackgroundImage.size};
[button addTarget:self action:@selector(goBack_Swizzle) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (void)goBack_Swizzle
{
[self.navigationController popViewControllerAnimated:YES];
}

经过试验,改写返回按钮会让苹果自带的返回手势失效,故开启手势

self.navigationController.interactivePopGestureRecognizer.delegate = self;
最近的文章

优秀博客收集

站在巨人的肩膀上,才能看的更高更远。在工作中不断收集优秀的博客,方便学习参考。总结如下 OneV’s Den 携程无线陈浩然 唐巧的技术博客 杨骑滔(KittenYang) 南峰子的技术博客 破船之家 NSHipster Limboy 无网不剩 Lex iOS notes 念茜的博客 Xcode Dev Ted’s Homepage txx’s blog KEVIN BLOG 阿毛的蛋疼地 亚庆的 Blog Nonomori 言无不尽 Wonderff...…

博客继续阅读
更早的文章

如何让Xcode打印日志中的unicode显示为中文

Xcode打印日志有时候显示的unicode,不是我们想要的中文,尤其是与后台调试接口的时候,面对乱麻麻的unicode,那是一个苦逼啊。解决方案为NSObject,NSString,NSDictionary,NSArray,NSSet添加分类.h文件中@interface debugging : NSObject@end@interface NSObject (DEBUGGING)+ (void)replaceClassMethodWithClass: (Class)clazz orig...…

Xcode继续阅读