Setting a Custom Image for a Back Button on UINavigationBar(为 UINavigationBar 上的后退按钮设置自定义图像)
本文介绍了为 UINavigationBar 上的后退按钮设置自定义图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为后退按钮设置一个自定义图像,当新视图被推入堆栈时,该按钮会自动放置到导航栏上.
I am trying to set a custom image for the back button that automatically gets place onto a navigation bar when a new view is pushed onto the stack.
我已经尝试在父viewController的viewDidLoad中添加以下内容:
I have tried adding the following in the viewDidLoad of the parent viewController:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
我还尝试了以下方法:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"BackButton.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = btn;
使用 UIAppearance 会产生非常奇怪的结果:
Using UIAppearance produces quite strange results:
推荐答案
试试这个代码
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
然后像这样定义 goback 方法
then define goback method like this
- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
这篇关于为 UINavigationBar 上的后退按钮设置自定义图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:为 UINavigationBar 上的后退按钮设置自定义图像
基础教程推荐
猜你喜欢
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
