如何隐藏默认导航栏按钮并在其中添加自定义按钮?

发布于 2024-10-23 13:42:42 字数 1068 浏览 1 评论 0原文

我有一个基于导航的应用程序,我想在其中隐藏默认导航 它显示在左侧,我想添加自己的自定义按钮。我编写了以下 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 技术交流群。

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

发布评论

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

评论(1

我很坚强 2024-10-30 13:42:42

您不应该隐藏后退按钮。如果替换 leftBarButtonItem,它应该覆盖那里的任何按钮。我使用这段代码,并且只有这段代码才能在我的几个应用程序中完成此任务。

UIBarButtonItem *btnCancel =
[[UIBarButtonItem alloc] initWithTitle: @"Cancel"
                                 style: UIBarButtonItemStylePlain
                                target: self
                                action: @selector(actionButtonCancel)];
self.navigationItem.leftBarButtonItem = btnCancel;
[btnCancel release];

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.

UIBarButtonItem *btnCancel =
[[UIBarButtonItem alloc] initWithTitle: @"Cancel"
                                 style: UIBarButtonItemStylePlain
                                target: self
                                action: @selector(actionButtonCancel)];
self.navigationItem.leftBarButtonItem = btnCancel;
[btnCancel release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文