UINavigationController中的自定义后退按钮 [英] Custom back button in UINavigationController

查看:161
本文介绍了UINavigationController中的自定义后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在开发的应用程序,我需要在导航栏中显示自定义后退按钮。我将按钮资源作为PNG图像,我正在编写此代码:

For an application I'm developing, I need to display a custom back button in a navigation bar. I have the button asset as a PNG image, and I'm writing this code:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 79, 29.0);
    [backButton setImage:[UIImage imageNamed:@"button_back.png"] forState:UIControlStateNormal];
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];

}

当我推这个视图时控制器,自定义按钮没有显示,而是我得到标准的后退按钮,里面有这个视图控制器的标题。

When I push this view controller, the custom button does not show up, and instead I get the standard back button with the title of this view controller inside.

我已经尝试过的事情:


  1. 通过将按钮添加到视图层次结构中,可以正确创建按钮 backButton 。它显示正确。

  2. 在同一方法中,更改了 navigationItem的 title 属性并确认它改变了(按预期)我的后退按钮的内容。

  1. Doubled check that the button backButton is created properly, by adding it to the view hierarchy. It displays properly.
  2. In the same method, changed the title property of the navigationItem and confirmed that it changes (as expected) the content of my back button.

任何人都可以看到我在做什么错误?有没有人成功使用自定义图像作为后退按钮打开 UINavigationController

Can anyone spot what I'm doing wrong? Did anyone succeed in using a custom image as the back button on with a UINavigationController?

推荐答案

backBarButtonItem 属性按预期工作,但它将始终根据导航栏色调颜色添加其标准按钮形状和颜色。

The backBarButtonItem property works as intended, but it will always add its standard button shape and color based on the navigation bar tint color.

您可以自定义文本,但不能替换图像。

You can customize the text, but not replace the image.

正如Andrew Pouliot建议的那样,一个解决方法是使用 leftBarButtonItem 相反,但我坚持使用标准按钮。

One workaround, as Andrew Pouliot suggested, is to use leftBarButtonItem instead, but I stuck to the standard button instead.

这篇关于UINavigationController中的自定义后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆