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

self.navigationItem.leftBarButtonItem = self.editButtonItem;

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

OR

像这样的东西?

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

当前回答

- (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或基于代码的方法中应用根视图控制器的样式。

其他回答

我写了一个扩展,使这更容易:

extension UIViewController {

    /// Convenience for setting the back button, which will be used on any view controller that this one pushes onto the stack
    @objc var backButtonTitle: String? {
        get {
            return navigationItem.backBarButtonItem?.title
        }
        set {
            if let existingBackBarButtonItem = navigationItem.backBarButtonItem {
                existingBackBarButtonItem.title = newValue
            }
            else {
                let newNavigationItem = UIBarButtonItem(title: newValue, style:.plain, target: nil, action: nil)
                navigationItem.backBarButtonItem = newNavigationItem
            }
        }
    }

}

我在viewDidLoad中应用了以下代码,它可以工作:

  // this will set the back button title
self.navigationController.navigationBar.topItem.title = @"Test";

 // this line set the back button and default icon color  

//[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor blackColor]];

this line change the back default icon to your custom icon
[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"menuicon"]]];

只是更新我使用矢量图标

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

你可以把这个类别放在AppDelegate中。m文件。 移除UINavigation中所有默认后退按钮的文本。只有箭头“<”或设置自定义图像,如果你需要。

objective - c:

...

@end

@implementation UINavigationItem (Extension)

- (UIBarButtonItem *)backBarButtonItem {

    return [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

@end

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

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

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