为 UINavigationController 制作自定义后退按钮

发布于 2025-01-06 03:25:17 字数 1568 浏览 1 评论 0原文

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

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

它也可以工作,但问题是为了使这项工作我需要调用它:

 - (void)viewDidAppear:(BOOL)animated ;

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

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

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

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

来自法国的感谢!

编辑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];
}

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 ;

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

(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: I never use IB, everything is made programmatically.

Thanks from France !

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难如初 2025-01-13 03:25:17

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

通常,执行此操作的方法是设置 ViewController AnavigationItem.backBarButtonItem

相反,您要做的是通过设置 ViewController BnavigationItem.leftBarButtonItem 在导航栏左侧提供一个自定义按钮。

您已经很好地实现了该方法,只是即使您没有设置 ViewController AnavigationItem.backBarButtonItem,默认情况下您仍然会获得默认的后退按钮。因此该按钮可能会显示在您的自定义后退按钮之上。

如果您设置 ViewController B 的 navigationItem.hidesBackButton = YES 那么您应该不会有任何问题。

将来,当您实现自定义后退按钮时,您应该通过设置 navigationItem.backBarButtonItem 而不是 navigationItem.leftBarButtonItem 来实现。您必须进行的一项调整是,使用这种方法,例如您可以使用 ViewController AnavigationItem 来更改 ViewController < 时显示的后退按钮。 strong>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.

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

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.

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.

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

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.

茶色山野 2025-01-13 03:25:17
UIBarButtonItem *backButton = [UIBarButtonItem new];

[backButton setTitle:@"Back"];

[[self navigationItem] setBackBarButtonItem:backButton];

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

享受!!!

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!!!

爱人如己 2025-01-13 03:25:17

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文