Custom back button in UINavigationController(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.
我已经尝试过的事情:
- 通过将按钮
backButton添加到视图层次结构中,双重检查按钮是否正确创建.它可以正常显示. - 以同样的方法,更改
navigationItem的title属性,并确认它更改(如预期)我的后退按钮的内容.
- Doubled check that the button
backButtonis created properly, by adding it to the view hierarchy. It displays properly. - In the same method, changed the
titleproperty of thenavigationItemand 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 中的自定义后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UINavigationController 中的自定义后退按钮
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
