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

self.navigationItem.leftBarButtonItem = self.editButtonItem;

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

OR

像这样的东西?

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

当前回答

你可以在viewWillDisappear上将当前ViewController的标题改为@"",当它即将再次显示时,将标题重新设置为之前的标题。

-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.title = @"Previous Title";
}

-(void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.title = @"";
}

其他回答

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Custom initialization
        self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

    }
    return self; 
}

就像Kyle Begeman所做的那样,在根视图控制器中添加上面的代码。所有子视图控制器都会被应用。此外,在initWithCoder:方法中添加这个,你可以在xib、storyboard或基于代码的方法中应用根视图控制器的样式。

你不能以你想要的方式访问导航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];
}

所有的答案都不能解决问题。在每个视图控制器中设置后退按钮标题,并且添加偏移量仍然会使下一个视图控制器标题向右移动,这是不可接受的。

这里是使用方法swizzling的方法,只需创建新的扩展UINavigationItem

import UIKit

extension UINavigationItem {
    public override class func initialize() {

    struct Static {
        static var token: dispatch_once_t = 0
    }

    // make sure this isn't a subclass
    if self !== UINavigationItem.self {
        return
    }

    dispatch_once(&Static.token) {
        let originalSelector = Selector("backBarButtonItem")
        let swizzledSelector = #selector(UINavigationItem.noTitleBackBarButtonItem)

        let originalMethod = class_getInstanceMethod(self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)

        let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

        if didAddMethod {
            class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
}

// MARK: - Method Swizzling

struct AssociatedKeys {
    static var ArrowBackButtonKey = "noTitleArrowBackButtonKey"
}

func noTitleBackBarButtonItem() -> UIBarButtonItem? {
    if let item = self.noTitleBackBarButtonItem() {
        return item
    }

    if let item = objc_getAssociatedObject(self, &AssociatedKeys.ArrowBackButtonKey) as? UIBarButtonItem {
        return item
    } else {
        let newItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
        objc_setAssociatedObject(self, &AssociatedKeys.ArrowBackButtonKey, newItem as UIBarButtonItem?, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        return newItem
    }
}
}

在你的第一个ViewController的prepareForSegue:方法中,你设置视图标题为@"",所以当下一个视图被推送时,它将显示前一个ViewController标题为@""。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    self.navigationItem.title = @" ";
}

唯一的问题是,当你点击返回按钮时,你之前的视图将没有标题,所以你可以在viewWillAppear上再次添加它:

-(void)viewWillAppear:(BOOL)animated{
   self.navigationItem.title = @"First View Title";
}

我不太喜欢这个解决方案,但它是可行的,我没有找到其他方法来做到这一点。

这是有效的,但它会删除前一项的标题,即使你弹出回到它:

self.navigationController.navigationBar.topItem.title = @"";

只需要在推送视图控制器的viewDidLoad上设置这个属性。