如何隐藏默认导航栏按钮并在其中添加自定义按钮?
我有一个基于导航的应用程序,我想在其中隐藏默认导航 它显示在左侧,我想添加自己的自定义按钮。我编写了以下 2 段代码。
代码示例 1:
- (void)viewDidLoad {
appDelegate=[(FoodAppDelegate *)[UIApplication sharedApplication]delegate];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"Volunteers_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 86, 30)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
但这会显示默认导航按钮。
第二个代码示例:
self.navigationItem.hidesBackButton=YES;
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
[barButton setImage:[UIImage imageNamed:@"Volunteers_back.png"]];
[self.navigationItem setLeftBarButtonItem:barButton];
此代码示例显示按钮默认值和自定义值彼此重叠。 有谁知道这里有什么问题?或者有任何示例代码可以实现此目的吗?
I have one navigation based application in which i want to hide the default navigation
which is display at left side and i want to add my own custom button.I have written following 2 code.
code sample 1:
- (void)viewDidLoad {
appDelegate=[(FoodAppDelegate *)[UIApplication sharedApplication]delegate];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"Volunteers_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 86, 30)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
But this displays default navigation button.
second code sample:
self.navigationItem.hidesBackButton=YES;
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
[barButton setImage:[UIImage imageNamed:@"Volunteers_back.png"]];
[self.navigationItem setLeftBarButtonItem:barButton];
This one displays both button default and custom overlapping each other.
Does anyone have any idea what is the problem here?or any sample code to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该隐藏后退按钮。如果替换 leftBarButtonItem,它应该覆盖那里的任何按钮。我使用这段代码,并且只有这段代码才能在我的几个应用程序中完成此任务。
You shouldn't have to hide the back button. If you replace the leftBarButtonItem, it should overwrite whatever button is there. I use this code, and ONLY this code to accomplish this in several of my apps.