ios 没有标题的 UINavigationBar 自定义后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18870128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:19:17  来源:igfitidea点击:

UINavigationBar custom back button without title

iosuser-interfaceuinavigationbar

提问by Kiddo

How can I customize the navigation back button in iOS 7 and above without title? (i.e. with the arrow only)

如何在没有标题的 iOS 7 及更高版本中自定义导航返回按钮?(即只有箭头)

self.navigationItem.leftBarButtonItem = self.editButtonItem;

I'm just wondering if they have any self.backButtonItem;

我只是想知道他们是否有任何 self.backButtonItem;

OR

或者

something like this?

像这样的东西?

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

回答by Kyle Begeman

It's actually pretty easy, here is what I do:

这实际上很容易,这是我要做的:

Objective C

目标 C

// Set this in every view controller so that the back button displays back instead of the root view controller name
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

Swift 2

斯威夫特 2

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)

Swift 3

斯威夫特 3

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

Put this line in the view controller that is pushing on to the stack (the previous view controller). The newly pushed view controller back button will now show whatever you put for initWithTitle, which in this case is an empty string.

将此行放在推送到堆栈的视图控制器中前一个视图控制器)。新按下的视图控制器后退按钮现在将显示您为 initWithTitle 设置的任何内容,在本例中为空字符串。

回答by Thomás Calmon

I found an easy way to make my back button with iOS single arrow.

我找到了一种使用 iOS 单箭头制作后退按钮的简单方法。

Let's supouse that you have a navigation controller going to ViewA from ViewB. In IB, select ViewA's navigation bar, you should see these options: Title, Prompt and Back Button.

让我们假设您有一个从 ViewB 转到 ViewA 的导航控制器。在 IB 中,选择 ViewA 的导航栏,您应该会看到这些选项:Title、Prompt 和 Back Button。

ViewA navigate bar options

ViewA 导航栏选项

ViewA navigate bar options

ViewA 导航栏选项

The trick is choose your destiny view controller back button title (ViewB) in the options of previousview controller (View A). If you don't fill the option "Back Button", iOS will put the title "Back" automatically, with previousview controller's title. So, you need to fill this option with a single space.

诀窍是在上一个视图控制器(视图 A)的选项中选择您的命运视图控制器后退按钮标题(视图 B)。如果您不填写“返回按钮”选项,iOS 会自动将标题“返回”与上一个视图控制器的标题一起放置。因此,您需要用一个空格填充此选项。

Fill space in "Back Button" option

填充“后退按钮”选项中的空间

Fill space in "Back Button" option

填充“后退按钮”选项中的空间

The Result:

结果:

The Result:

结果:

回答by mt81

Just use an image!

只需使用图像!

OBJ-C:

OBJ-C:

- (void)viewDidLoad {
     [super viewDidLoad];
     UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Icon-Back"]
                                                                                        style:UIBarButtonItemStylePlain
                                                                                       target:self.navigationController
                                                                                       action:@selector(popViewControllerAnimated:)];
     self.navigationItem.leftBarButtonItem = backButton;
}

SWIFT 4:

快速 4

let backBTN = UIBarButtonItem(image: UIImage(named: "Back"), 
                              style: .plain, 
                              target: navigationController, 
                              action: #selector(UINavigationController.popViewController(animated:)))
navigationItem.leftBarButtonItem = backBTN
navigationController?.interactivePopGestureRecognizer?.delegate = self

Icon-Back.png

图标返回.png

Icon-Back

图标返回

[email protected]

图标返回@2x.png

Icon-Back@2x

图标返回@2x

[email protected]

图标返回@3x.png

Icon-Back@23x

图标返回@23x

回答by Anibal Itriago

iOS7 has new interface rules, so It's better to keep at least the back arrow when you push a UIView. It's very easy to change the "back" text programmatically. Just add this code before push the view (Or prepareForSegue if you are using StoryBoards):

iOS7有新的界面规则,所以推送一个UIView时最好至少保留后退箭头。以编程方式更改“返回”文本非常容易。只需在推送视图之前添加此代码(如果您使用的是 StoryBoards,则为 prepareForSegue):

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
      self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"NEW TITLE" style:UIBarButtonItemStylePlain target:nil action:nil];
}

This will change the default "Back" text, but will keep the iOS7 styled back arrow. You can also change the tint color for the back arrow before push the view:

这将更改默认的“后退”文本,但会保留 iOS7 样式的后退箭头。您还可以在推动视图之前更改后退箭头的色调:

- (void)viewDidLoad{
     //NavBar background color:
     self.navigationController.navigationBar.barTintColor=[UIColor redColor];
//NavBar tint color for elements:
     self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
}

Hope this helps you!

希望这对你有帮助!

回答by Swaroop S

Nothing much you need to do. You can achieve the same through storyboard itself.

您无需做太多事情。您可以通过故事板本身实现相同的目标。

Just go the root Navigation controller and give a space. Remember not to the controller you wanted the back button without title, but to the root navigation controller.

只需转到根导航控制器并留出空间即可。记住不是你想要没有标题的后退按钮的控制器,而是根导航控制器。

As per the image below. This works for iOS 7 and iOS 8

如下图所示。 这适用于 iOS 7 和 iOS 8

回答by secondcitysaint

While Kyle Begeman's answertotally does the trick, it is quite annoying to have this code in every view controller possible. I ended up with a simple UINavigationItemcategory. Beware, here be dragons! Sorry, I mean, swizzling:

虽然Kyle Begeman 的回答完全可以解决问题,但在每个可能的视图控制器中都有这段代码是很烦人的。我最终得到了一个简单的UINavigationItem类别。当心,这里有龙!对不起,我的意思是,swizzling:

#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

回答by hiroshi

EDIT: 2014-04-09: As I gained reputations, I feel sorry because I don't use this trick any more. I recommend Kyle's answer. Also notice that the selfof self.navigationItem.backBarButtonItemisn't the view controller the back button is displayed, but the previous view controller to be went back.

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

If you don't need to have title text for the previous view controller, just fill the title with a blank string;

如果你不需要为之前的视图控制器添加标题文本,只需用空字符串填充标题即可;

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

This will prevent showing "back" with chevron on the pushed view controller.

这将防止在推送的视图控制器上用 V 形显示“返回”。

EDIT: Even you use non-blank title text, setting the title of the previous view controller in viewWillAppear:works except the title can flicker in a blink when view controller popped. I think "The twitter app" seems to do more subtle hack to avoid the flicker.

编辑:即使您使用非空白标题文本,在viewWillAppear:作品中设置前一个视图控制器的标题,但在视图控制器弹出时标题可能会闪烁。我认为“推特应用程序”似乎做了更微妙的黑客攻击来避免闪烁。

回答by Guto Araujo

This works, but it will remove the title of the previous item, even if you pop back to it:

这有效,但它会删除上一个项目的标题,即使您弹回它:

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

Just set this property on viewDidLoadof the pushed View Controller.

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

回答by r_19

This is how I do it and the simplest, works and most clear way to do it.

这就是我这样做的方式,也是最简单、有效和最清晰的方法。

This works if embed on Navigation Controller

如果嵌入,这有效 Navigation Controller

Swift 3

斯威夫特 3

In viewDidLoadI add this to the View Controller you want the back button to be just arrow.

viewDidLoad我将此添加到视图控制器中,您希望后退按钮只是箭头。

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

The difference of this to @Kyle Begeman's answer is that you call this on the view controller that you want the back button to be just arrow, not on the pushing stack view controller.

这与@Kyle Begeman 的答案的不同之处在于,您在视图控制器上调用它,您希望后退按钮只是箭头,而不是推送堆栈视图控制器。

回答by Nino Bouchedid

SWIFT 4

快速 4

For those looking to create custom back buttons as well as have their title removed please use the following piece of code within the view controller that's pushing the new one:

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

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)

For a more universal use, do the following:

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

  1. 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)
    }
    
  2. Then use it in the view controllers as follows:

    addCustomizedBackBtn(navigationController: self.navigationController, navigationItem: self.navigationItem)
    
  1. 创建一个通用函数如下:

    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)
    }
    
  2. 然后在视图控制器中使用它,如下所示:

    addCustomizedBackBtn(navigationController: self.navigationController, navigationItem: self.navigationItem)