iphone - 后退按钮的自定义 UIBarButtonItem
我正在尝试为导航栏中的后退按钮使用自定义项目。
UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem setBackBarButtonItem: customItem];
[customItem release];
我最终得到的是我的图像,周围有边框。它看起来像这样(我的图像是后退按钮):
如何摆脱边框?我做错了什么?
I am trying to use a custom item for the back button in my navigation bar.
UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem setBackBarButtonItem: customItem];
[customItem release];
What I end up getting is my image with a border around it. It looks like this (My image is the back button):
How can I get rid of the border? What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的图像出现在后退按钮内部,并且它显然(从您的屏幕截图来看)与后退按钮的大小不同。
您可能想隐藏后退按钮,然后将其替换为“左栏按钮”。
代码:
Your image is appearing inside of a back button and it is apparently (from your screenshot) not the same size as the back button.
You might want to hide the back button and then replace it with a "Left Bar Button" instead.
Code:
这是我的解决方案,基于 Jorge 的代码。
我在
UIViewController
上创建了一个简单的类别:UIViewController+ImageBackButton.h
UIViewController+ImageBackButton.m
现在您所要做的就是
#在所有视图控制器或其他视图控制器继承的自定义基本视图控制器类中导入 UIViewController+ImageBackButton.h
并实现viewWillAppear:
方法:仅此而已。现在你到处都有一个图像后退按钮。无国界。享受!
Building on Jorge's code, this is my solution.
I create a simple category on
UIViewController
:UIViewController+ImageBackButton.h
UIViewController+ImageBackButton.m
Now all you have to do is
#import UIViewController+ImageBackButton.h
in either all of your view controllers or in a custom base view controller class that your other view controllers inherit from and implement theviewWillAppear:
method:That's all. Now you have an image back button everywhere. Without a border. Enjoy!
这是更新版本。这包括设置目标、字体大小等。
此外,它还反映出
setHidesBackButton
不能作为navigationController
的属性使用。请注意,这是来自 ARC 项目,因此对象上没有发布等。
Here's an updated version. This includes the setting the target, font size, etc.
Also, it reflects that
setHidesBackButton
is not available as a property ofnavigationController
.Note that this is from an ARC project, so no releases etc. on the objects.
我创建了一个 UINavigationBar 类别,在每个 viewController 中的 viewWillAppear 中调用它。我用来更改后退按钮外观的代码如下:
在 iOS 6 下完美运行。
I created a category of UINavigationBar which I call in viewWillAppear in each of my viewControllers. The code that I use to change the appearance of my back button is the following:
Works perfectly under iOS 6.