ios 为 UINavigationController 制作自定义后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9300503/
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-30 16:52:43  来源:igfitidea点击:

Make a custom back button for UINavigationController

iosuinavigationcontrolleruinavigationbaruibarbuttonitemviewdidappear

提问by Trivane

I'm developping an app for iOS 4.2+. I subclassed my UINavigationControllerto insert two UIImageViewand make the navigation bar look custom. Everything is working great but I have a little problem. I created custom UIBarButtonItemand inside my view controllers i put them with :

我正在为 iOS 4.2+ 开发一个应用程序。我将 my 子类化为UINavigationController插入两个UIImageView并使导航栏看起来自定义。一切都很好,但我有一个小问题。我创建了自定义UIBarButtonItem并在我的视图控制器中放置了它们:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];

It works too but the problem is that to make this work I need to call it from:

它也可以工作,但问题是要完成这项工作,我需要从以下位置调用它:

 - (void)viewDidAppear:(BOOL)animated ;

So it only appears after the animation and I can see the non customized button 1 second before the customized one replace it.

所以它只出现在动画之后,我可以在自定义按钮替换它之前 1 秒看到非自定义按钮。

(I tried with viewWillAppearbut then nothing append in navigationbar)

(我尝试过,viewWillAppear但导航栏中没有附加任何内容)

I would like to know if you have a solution that could correct this problem.

我想知道您是否有可以解决此问题的解决方案。

PS: I never use IB, everything is made programmatically.

PS:我从不使用 IB,一切都是以编程方式制作的。

Thanks from France !

来自法国的感谢!

EDIT 1: here is the code that didn't show anything for viewWillAppear:

编辑 1:这是没有显示任何内容的代码viewWillAppear

- (void)viewWillAppear:(BOOL)animated  {
    [self setTitle:@"Jeu"];

    //Bouton testflight
    TIFButton *testFeedbackButton = [[TIFButton alloc]init];
    [testFeedbackButton setTitle: @"Envoyer un Feedback" forState:UIControlStateNormal];
    [testFeedbackButton addTarget:self action:@selector(launchFeedback) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *testFeedback = [[UIBarButtonItem alloc] initWithCustomView:testFeedbackButton];

    self.navigationItem.rightBarButtonItem = testFeedback;  

    TIFBackButton *backButton = [[TIFBackButton alloc]init];
    [backButton setTitle: @"Retour" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popToPrecedentViewController) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
}

回答by yuji

Let's say you have two ViewControllers, A and B, you're pushing B onto the stack when A is topmost, and you want to customize the back button that shows up when B is on top.

假设您有两个 ViewController,A 和 B,当 A 位于最顶部时将 B 推入堆栈,并且您想自定义当 B 位于顶部时显示的后退按钮。

Typically, the way to do this is to set ViewController A's navigationItem.backBarButtonItem.

通常,这样做的方法是设置 ViewController AnavigationItem.backBarButtonItem.

Instead, what you're doing is to give ViewController Ba custom button on the left side of the navbar by setting its navigationItem.leftBarButtonItem.

相反,您正在做的是通过设置ViewController B在导航栏左侧的自定义按钮navigationItem.leftBarButtonItem.

You've implemented that approach fine, except that even if you don't set ViewController A's navigationItem.backBarButtonItem, by default you still get a default back button as well. So that button is probably showing up on top of your custom back button.

你已经实现了这种做法很好,除了,即使您没有设置的ViewController一个navigationItem.backBarButtonItem,默认情况下,你仍然可以获得一个默认的后退按钮以及。因此该按钮可能显示在您的自定义后退按钮之上。

If you set ViewController B's navigationItem.hidesBackButton = YESthen you shouldn't have any problem.

如果您设置了 ViewController B,navigationItem.hidesBackButton = YES那么您应该没有任何问题。

And in the future, when you implement custom back buttons, you should do it by setting navigationItem.backBarButtonIteminstead of navigationItem.leftBarButtonItem. The one adjustment you'll have to make is that with this approach, for example you would use ViewController A's navigationItemto change the back button that shows up when ViewController Bis on top.

将来,当您实现自定义后退按钮时,您应该通过设置navigationItem.backBarButtonItem而不是navigationItem.leftBarButtonItem. 在一个调整,你必须提出的是,使用这种方法,例如你可以使用的ViewController一个navigationItem来换按钮时显示的ViewController是在上面。

回答by Prithivi

UIBarButtonItem *backButton = [UIBarButtonItem new];

[backButton setTitle:@"Back"];

[[self navigationItem] setBackBarButtonItem:backButton];

This code you have to mentioned in previous view controller then only it default back button came in next view controller.

您必须在之前的视图控制器中提到此代码,然后只有默认的后退按钮出现在下一个视图控制器中。

Enjoy!!!

享受!!!

回答by Johannes Fahrenkrug

I've come up with a somewhat universal solution: https://stackoverflow.com/a/16831482/171933It involves a category on UIViewController and requires minimal code to have an image back button on all your screens.

我想出了一个有点通用的解决方案:https: //stackoverflow.com/a/16831482/171933它涉及 UIViewController 上的一个类别,需要最少的代码才能在所有屏幕上都有一个图像后退按钮。