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

self.navigationItem.leftBarButtonItem = self.editButtonItem;

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

OR

像这样的东西?

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

当前回答

斯威夫特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)

其他回答

编辑:2014-04-09:当我获得声誉时,我感到抱歉,因为我不再使用这个技巧了。我推荐凯尔的答案。还要注意self。navigationitem。backbarbuttonitem的self不是返回按钮显示的视图控制器,而是要返回的前一个视图控制器。

如果之前的视图控制器不需要标题文本,只需用空白字符串填充标题;

self.navigationItem.title = @"";
[self.navigationController pushViewController:viewController animated:YES];

这将防止在被推送的视图控制器上显示带雪佛龙的“返回”。

编辑:即使你使用非空白标题文本,在viewWillAppear中设置前一个视图控制器的标题:除了标题可以闪烁在闪烁时,视图控制器弹出。我认为“推特应用程序”似乎做了更微妙的hack来避免闪烁。

这在iOS6和ios7上都很有效

Xamarin (c#)选项

var buttonStyleItems = UIBarButtonItem.AppearanceWhenContainedIn(typeof(SettingsNavigationController));
buttonStyleItems.SetBackButtonTitlePositionAdjustment(new UIOffset(-1000, -1000), UIBarMetrics.Default);

objective - c选项

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

这就是我怎么做的,也是最简单,最有效,最清晰的方法。

如果嵌入在导航控制器上,这是有效的

斯威夫特3

在viewDidLoad中,我把这个添加到视图控制器中,你想让后退按钮只是箭头。

if let topItem = self.navigationController?.navigationBar.topItem {
   topItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}

这个和@Kyle Begeman的回答的不同之处在于你在视图控制器上调用这个你想让后退按钮只是箭头,而不是在push stack视图控制器上。

如果你设置了导航栏的tintColor,添加一个没有标题的自定义后退按钮图像,该图像的tintColor将反映图像的颜色。请点击苹果文档链接。

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/index.html#//apple_ref/doc/uid/TP40012857-UIView-SW7

UINavigationItem *navItem = [[UINavigationItem alloc] init];
   navBar.tintColor = self.tintColor;

   UIImage *myImage = [UIImage imageNamed:@"left_arrow.png"];
     myImage = [myImage              imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonFunction:)];
     navItem.leftBarButtonItem = leftButton;

    navBar.items = @[ navItem ];

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

这里是使用方法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
    }
}
}