为 UINavigationController 制作自定义后退按钮
我正在开发一个适用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您有两个 ViewController,A 和 B,当 A 位于最顶层时,您将 B 推入堆栈,并且您想要自定义当 B 位于最顶层时显示的后退按钮。
通常,执行此操作的方法是设置 ViewController A 的
navigationItem.backBarButtonItem
。相反,您要做的是通过设置 ViewController B 的
navigationItem.leftBarButtonItem
在导航栏左侧提供一个自定义按钮。您已经很好地实现了该方法,只是即使您没有设置 ViewController A 的
navigationItem.backBarButtonItem
,默认情况下您仍然会获得默认的后退按钮。因此该按钮可能会显示在您的自定义后退按钮之上。如果您设置 ViewController B 的
navigationItem.hidesBackButton = YES
那么您应该不会有任何问题。将来,当您实现自定义后退按钮时,您应该通过设置
navigationItem.backBarButtonItem
而不是navigationItem.leftBarButtonItem
来实现。您必须进行的一项调整是,使用这种方法,例如您可以使用 ViewController A 的navigationItem
来更改 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 ofnavigationItem.leftBarButtonItem
. The one adjustment you'll have to make is that with this approach, for example you would use ViewController A'snavigationItem
to change the back button that shows up when ViewController B is on top.您必须在上一个视图控制器中提到此代码,然后只有它的默认后退按钮出现在下一个视图控制器中。
享受!!!
This code you have to mentioned in previous view controller then only it default back button came in next view controller.
Enjoy!!!
我想出了一个有点通用的解决方案: 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.