自定义 UINavigationBar 按钮不显示

发布于 2024-10-09 00:57:12 字数 1767 浏览 0 评论 0原文

(编辑:在底部添加更多信息)

首先,我浏览了类似的帖子并尝试了他们的答案,但没有运气:(我有一个自定义导航栏(只是一个自定义背景图像),我正在尝试添加一个自定义后退按钮 上面的代码是为了添加按钮,

// custom back button.
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backImg = [UIImage imageNamed:@"btn_back.png"];
backBtn.titleLabel.text = @"";
[backBtn setBackgroundImage:backImg forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backPressed) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 33);

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem.title = @"";

但它不会显示在导航栏上,我仍然可以单击它应该出现的区域,它确实会让我回到之前的视图?!删除上面的代码会显示标准。 有什么想法吗?提前

谢谢!

(编辑)

大家好,

我尝试了你们的所有建议,但仍然没有成功:(我可能应该之前提到过,我有一个使用以下代码的自定义导航栏图像。影响我的自定义后退按钮图像的显示。

此代码根据我设置的参数显示自定义图像。再次感谢您的进一步帮助!

// custom navigation bar image
@implementation UINavigationController (CustomImage)

// set the background image for the nav bar.
- (void) setCustomNavBar:(NSInteger)screen {

UIImage *navBarImg;
switch (screen) {
    case kCreateHuntScreenIdentifier:
        navBarImg = [UIImage imageNamed:@"title_create-hunt.png"];
        break;

    case kCreateLocationsScreenIdentifier:
        navBarImg = [UIImage imageNamed:@"title_create-location.png"];
        break;

    case kListNewLocationsScreenIdentifier:
        navBarImg = [UIImage imageNamed:@"title_choose-location.png"];
        break;

    default:
        break;
}

UIImageView *imgView = [[UIImageView alloc] initWithImage:navBarImg];
[[[self.navigationBar subviews] objectAtIndex:0] removeFromSuperview];
[self.navigationBar addSubview:imgView];
[imgView release];
}

@end

(edited: added more information at bottom)

Firstly I have looked through similar posts and tried their answers but with no luck :( I have a custom navigation bar (just a custom background image) and I'm trying to add a custom back button.

// custom back button.
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backImg = [UIImage imageNamed:@"btn_back.png"];
backBtn.titleLabel.text = @"";
[backBtn setBackgroundImage:backImg forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backPressed) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 33);

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem.title = @"";

The above code is meant to add the button but it does not display on the nav bar. I can still click the area where its meant to appear and it does bring me back to the previous view?! removing the above code displays the standard back button.

Any ideas? Thanks in advance!

(edit)

Hi again guys,

I tried all your suggestions and still no luck :( I probably should have mentioned earlier that I have a custom nav bar image using the following code. This might be affecting the display of my custom back button image.

This code displays a custom image depending upon a parameter I set. Thanks again for further help!

// custom navigation bar image
@implementation UINavigationController (CustomImage)

// set the background image for the nav bar.
- (void) setCustomNavBar:(NSInteger)screen {

UIImage *navBarImg;
switch (screen) {
    case kCreateHuntScreenIdentifier:
        navBarImg = [UIImage imageNamed:@"title_create-hunt.png"];
        break;

    case kCreateLocationsScreenIdentifier:
        navBarImg = [UIImage imageNamed:@"title_create-location.png"];
        break;

    case kListNewLocationsScreenIdentifier:
        navBarImg = [UIImage imageNamed:@"title_choose-location.png"];
        break;

    default:
        break;
}

UIImageView *imgView = [[UIImageView alloc] initWithImage:navBarImg];
[[[self.navigationBar subviews] objectAtIndex:0] removeFromSuperview];
[self.navigationBar addSubview:imgView];
[imgView release];
}

@end

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

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

发布评论

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

评论(3

披肩女神 2024-10-16 00:57:13

再次感谢您的所有建议!我终于找到了一个解决方案:D

很快,当我推送或弹出堆栈时,我需要能够将导航栏的背景图像从一个视图更改为另一个视图。

使用最流行的方法,例如覆盖 UINavigationBar 的 drawRect 只能部分工作,它可以在根视图上工作,但然后将图像推到每个其他屏幕的导航栏堆栈的前面,无论我是否将其发送到后面, 我在这里找到了解决方案

http://sebastiancelis.com/ 2009/12/21/adding-background-image-uinavigationbar/

读一读,效果很好:)

Thanks again for all your advice! I finally found a solution :D

Very quickly I needed to be able to change the background image for a navigation bar from view to view when I pushed or popped the stack.

Using the most popular methods such as overriding the UINavigationBar's drawRect would only partly work, it would work on the root view but then push the image to the front of the navigation bar's stack for every other screen no matter if I sent it to the back, etc.

I found the solution here: http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/

Take a read, it works great :)

你又不是我 2024-10-16 00:57:13
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"imgname.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(method_name:) forControlEvents:UIControlEventTouchUpInside];
btn.frame=CGRectMake(3, 2, 53, 30);
UIBarButtonItem *btnBack=[[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem=btnBack;
[btnBack release];
[btn release];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"imgname.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(method_name:) forControlEvents:UIControlEventTouchUpInside];
btn.frame=CGRectMake(3, 2, 53, 30);
UIBarButtonItem *btnBack=[[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.leftBarButtonItem=btnBack;
[btnBack release];
[btn release];
一江春梦 2024-10-16 00:57:13

尝试,

self.navigationItem.hidesBackButton = YES;

在编写 .leftBarButtonItem 的代码之前。希望它有帮助

Try,

self.navigationItem.hidesBackButton = YES;

befor writing the code for .leftBarButtonItem .Hope it helps

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