为UINavigationController创建一个自定义后退按钮 [英] Make a custom back button for UINavigationController

查看:102
本文介绍了为UINavigationController创建一个自定义后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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 ;

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

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

(我尝试使用 viewWillAppear 但在导航栏中没有任何附加内容)

(I tried with viewWillAppear but then nothing append in navigationbar)

我想知道你是否有解决方案可以解决这个问题。

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

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

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

感谢法国!

编辑1:这里的代码没有显示 viewWillAppear

EDIT 1: here is the code that didn't show anything for 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];
}


推荐答案

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

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 navigationItem.backBarButtonItem

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

相反,您正在做的是通过设置 navigationItem.leftBarButtonItem B 提供一个自定义按钮>。

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

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

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 B的 navigationItem.hidesBackButton = YES 然后你应该没有任何问题。

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

将来,当你实现自定义后退按钮时,你应该通过设置 navigationItem.backBarButtonItem 而不是 navigationItem.leftBarButtonItem 。您需要进行的一项调整是,使用此方法,例如,您将使用ViewController A navigationItem 来更改后退按钮当ViewController B 位于顶部时显示。

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

这篇关于为UINavigationController创建一个自定义后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆