我如何自定义导航返回按钮在iOS 7及以上没有标题?(即只使用箭头)

self.navigationItem.leftBarButtonItem = self.editButtonItem;

我只是想知道它们是否有self。backbuttonitem;

OR

像这样的东西?

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
                   initWithBarButtonSystemItem:UIBarButtonSystemItemBACK 
                   target:self action:@selector(back)];

当前回答

添加新的UIBarButtonItem到左边的UINavigationItem 改变UIBarButtonItem的使用方式 现在,点击UINavigationItem,并从outlets滑动barBackButtonItem到左UIBarButtonItem

其他回答

斯威夫特4

对于那些希望创建自定义后退按钮以及删除它们的标题的人,请在视图控制器中使用下面的一段代码来推送新的按钮:

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "close")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "close")
self.navigationItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

为了更普遍的使用,请执行以下操作:

Create a universal function as follows: func addCustomizedBackBtn(navigationController: UINavigationController?, navigationItem: UINavigationItem?) { navigationController?.navigationBar.backIndicatorImage = UIImage(named: "close") navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "close") navigationItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil) } Then use it in the view controllers as follows: addCustomizedBackBtn(navigationController: self.navigationController, navigationItem: self.navigationItem)

改变backItem。title = "" to使用topItem。标题= "" 设置navigationItem。hidesBackButton = true & navigationItem。leftBarButtonItem将失去后退手势 记住,我们必须创建2个back映像的实例

我的解决方案将改变图像和保持后面的手势:

navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "back")
navigationController?.navigationBar.topItem?.title = ""

你不能以你想要的方式访问导航backButtonItem,你需要创建自己的返回按钮,如下所示:

- (void)loadView
{
    [super loadView];
    UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 44.0f, 30.0f)];
    [backButton setImage:[UIImage imageNamed:@"back.png"]  forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

当然了:

- (void) popVC{
  [self.navigationController popViewControllerAnimated:YES];
}

这招对我很管用

[[UIBarButtonItem appearance] 
setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000) 
forBarMetrics:UIBarMetricsDefault];

愿一切都好!

虽然Kyle Begeman的回答完全做到了,但在每个视图控制器中都使用这种代码是相当烦人的。我最终得到了一个简单的UINavigationItem类别。小心,这里有龙!抱歉,我的意思是:

#import <objc/runtime.h>

@implementation UINavigationItem (ArrowBackButton)

static char kArrowBackButtonKey;

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method m1 = class_getInstanceMethod(self, @selector(backBarButtonItem));
        Method m2 = class_getInstanceMethod(self, @selector(arrowBackButton_backBarButtonItem));
        method_exchangeImplementations(m1, m2);
    });
}

- (UIBarButtonItem *)arrowBackButton_backBarButtonItem {
    UIBarButtonItem *item = [self arrowBackButton_backBarButtonItem];
    if (item) {
        return item;
    }

    item = objc_getAssociatedObject(self, &kArrowBackButtonKey);
    if (!item) {
        item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:NULL];
        objc_setAssociatedObject(self, &kArrowBackButtonKey, item, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return item;
}

@end