出现这个问题的原因:由于设置nav返回不需要文字
//注:添加此方法,切换应用会出现一闪
[barItem setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin,NSIntegerMin)forBarMetrics:UIBarMetricsDefault];
解决方法:
第一种方法:在parent viewController中的viewDidLoad配置如下:
- (void)viewDidLoad {
[superviewDidLoad];
//设置返回不需要文字
UIBarButtonItem *backButtonItem = [[UIBarButtonItemalloc]initWithTitle:@""style:UIBarButtonItemStylePlaintarget:nilaction:nil];
[self.navigationItemsetBackBarButtonItem:backButtonItem];
}
第二种方法:黑盒子,利用运行时来替换viewDidLoad方法,即写一个分类,具体如下:
#import <UIKit/UIKit.h>
@interface UIViewController (EmptyBackButton)
@end
#import "UIViewController+EmptyBackButton.h"
#import <objc/runtime.h>
@implementation UIViewController (EmptyBackButton)
+ (void)load {
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [selfclass];
SEL originalSelector =@selector(viewDidLoad);
SEL swizzledSelector =@selector(mob_viewDidLoad);
Method originalMethod =class_getInstanceMethod(class, originalSelector);
Method swizzledMethod =class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =class_addMethod(class, originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class, swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)mob_viewDidLoad {
[selfmob_viewDidLoad];
UIBarButtonItem *backButtonItem = [[UIBarButtonItemalloc]initWithTitle:@""style:UIBarButtonItemStylePlaintarget:nilaction:nil];
[self.navigationItemsetBackBarButtonItem:backButtonItem];
}
参考链接:http://justabeech.com/2014/02/24/empty-back-button-on-ios7/