如何使用UINavigationController的自定义视图创建backBarButtomItem [英] How to create backBarButtomItem with custom view for a UINavigationController
问题描述
我有一个 UINavigationController
,其中我推几个视图。 viewDidLoad
对于其中一个视图,我想将 self.navigationItem.backBarButtonItem
设置为自定义视图自定义图像)。我不知道为什么,但它似乎不工作。
UIButton * backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,63,30)];
[backButton setImage:[UIImage imageNamed:@back_OFF.png] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@back_ON.png] forState:UIControlStateSelected];
UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.backBarButtonItem = backButtonItem;
[backButtonItem release];
[backButton release];
我测试了一个标准的标题,它工作。
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@Prout style:UIBarButtonItemStyleDone target:nil action:nil] autorelease];感谢您对此的任何帮助。
。>解决方案我相当确定
backBarButtonItem
是一个只读属性。而不是修改backBarButtonItem
,尝试设置自定义leftBarButtonItem
并隐藏backBarButtonItem
:self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@Proutstyle:UIBarButtonItemStyleDone target:nil action:nil] autorelease];
self.navigationItem.hidesBackButton = YES;
您还需要确保挂接自定义按钮, code> UINavigationBar
I have a
UINavigationController
into which I push several views. InsideviewDidLoad
for one of these views I want to set theself.navigationItem.backBarButtonItem
to a custom view (based on a custom image). I don't know why, but it doesn't seem to work. Instead, I get the standard "back" button.UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 63, 30)]; [backButton setImage:[UIImage imageNamed:@"back_OFF.png"] forState:UIControlStateNormal]; [backButton setImage:[UIImage imageNamed:@"back_ON.png"] forState:UIControlStateSelected]; UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; self.navigationItem.backBarButtonItem = backButtonItem; [backButtonItem release]; [backButton release];
I tested with a standard title and it worked. What is wrong with the above code ?
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Prout" style:UIBarButtonItemStyleDone target:nil action:nil] autorelease];
Thanks for any help on this.
解决方案I'm fairly certain that the
backBarButtonItem
is a read-only property. Instead of modifying thebackBarButtonItem
, try setting a customleftBarButtonItem
and hide thebackBarButtonItem
:self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Prout" style:UIBarButtonItemStyleDone target:nil action:nil] autorelease]; self.navigationItem.hidesBackButton = YES;
You will also need to make sure you hook up the custom button to call the back action on the
UINavigationBar
.这篇关于如何使用UINavigationController的自定义视图创建backBarButtomItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!