如何摆脱导航栏后退按钮的标题“返回”?

时间:2014-11-19 14:15:23

标签: ios objective-c cocoa-touch uinavigationbar

如何摆脱导航栏后退按钮的标题“返回”?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Nav Back Button

    UIImage *backButtonIcon = [UIImage imageNamed:@"back_black"];

    [UINavigationBar appearance].backIndicatorImage = backButtonIcon;
    [UINavigationBar appearance].backIndicatorTransitionMaskImage = backButtonIcon;

    return YES;
}

图片here

4 个答案:

答案 0 :(得分:0)

你可以将前一个viewController的title属性设置为一个空字符串,它就可以了。

答案 1 :(得分:0)

试试这个

 self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Custom Title"
        style:UIBarButtonItemStyleBordered
       target:nil
       action:nil];

答案 2 :(得分:0)

您需要将上一个视图控制器的后退按钮设置为这样的空字符串。

UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButtonItem];

但如果它是一个应用程序范围内的通缉行为,那么你可以在基类UIViewController子类中进行,或者通过调配来实现:)

<强>的UIViewController + EmptyBackButton.h

@interface UIViewController (EmptyBackButton)

@end

<强>的UIViewController + EmptyBackButton.m

#import "UIViewController+EmptyBackButton.h"
#import <objc/runtime.h>

@implementation UIViewController (EmptyBackButton)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        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);
        }
    });
}

#pragma mark - Method Swizzling

- (void)mob_viewDidLoad {
    [self mob_viewDidLoad];
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:backButtonItem];
}

@end

这会将空的后退按钮添加到UIViewControllerviewDidLoad的所有子类。

另外,您可以查看我之前写过的关于Empty Back Buttons的博客文章。

答案 3 :(得分:0)

UIView *backBtnView = [[UIView alloc] initWithFrame:CGRectMake(0,0,14,36)];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setFrame:CGRectMake(0,0,14,36)];
[backBtn setImage:[UIImage imageNamed:@"yourImageName"] forState:UIControlStateNormal];
[backBtn setEnabled:YES];
[backBtnView addSubview:backBtn];
UIBarButtonItem* barbackBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtnView];
self.navigationItem.leftBarButtonItem = barbackBtn;
[backBtn addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
backBtn.adjustsImageWhenHighlighted = NO;