为 UINavigationController 制作自定义后退按钮 [英] Make a custom back button for UINavigationController

查看:19
本文介绍了为 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];
}

推荐答案

假设您有两个 ViewController,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 AnavigationItem.backBarButtonItem.

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

相反,您所做的是通过设置其 navigationItem.leftBarButtonItem 在导航栏左侧为 ViewController 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 AnavigationItem.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 AnavigationItem 更改 ViewController <时显示的后退按钮strong>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天全站免登陆