UIBarButtonItem具有自定义图像,没有边框 [英] UIBarButtonItem with custom image and no border

查看:136
本文介绍了UIBarButtonItem具有自定义图像,没有边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带有自定义图像的UIBarButtonItem,但我不想要iPhone添加的边框,因为我的图像有一个特殊的边框。

I want to create a UIBarButtonItem with a custom image, but I don't want the border that iPhone adds, as my Image has a special border.

它是与后退按钮相同但是前进按钮。

It's the same as the back button but a forward button.

此应用程序适用于inHouse项目,因此我不关心Apple是拒绝或批准它还是喜欢它: - )

This App is for an inHouse project, so I don't care if Apple reject or approves it or likes it :-)

如果我使用UIBarButtonItem的initWithCustomView:v属性,我可以这样做:

If I use the initWithCustomView:v property of the UIBarButtonItem, I can do it:

UIImage *image = [UIImage imageNamed:@"right.png"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
[button setBackgroundImage: [[UIImage imageNamed: @"right_clicked.png"] stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateHighlighted];

 button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);

[button addTarget:self action:@selector(AcceptData)    forControlEvents:UIControlEventTouchUpInside];

UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];

[v addSubview:button];

UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithCustomView:v];

self.navigationItem.rightBarButtonItem= forward;

[v release];
[image release];

这样可行,但如果我必须在10个视图中重复此过程,这不是DRY。

This works, but if I have to repeat this process in 10 views, this is not DRY.

我想我必须继承,但是什么?

I suppose I have to subclass, but what ?


  • NSView?

  • UIBarButtonItem?

谢谢,

问候,

推荐答案

您可以向UIBarButtonItem添加方法,而无需使用自定义类别对其进行子类化:

You can add a method to UIBarButtonItem without subclassing it using custom category:

@interface UIBarButtonItem(MyCategory)

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action;

@end

@implementation UIBarButtonItem(MyCategory)

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{
 // Move your item creation code here
}
@end

因此,您可以在代码中的任何位置创建调用此方法的条形项(前提是您包含带声明的标题)。

So anywhere in your code you can create bar item calling this method (provided that you include a header with its declaration).

PS您不需要使用'v'UIView,因为您可以直接使用按钮创建 UIBarButtonItem

P.P.S.您还需要在代码中使用[正向发布]。

P.S. You do not need to use 'v' UIView as you can create UIBarButtonItem with a button as custom view directly.
P.P.S. You also need [forward release] in your code.

这篇关于UIBarButtonItem具有自定义图像,没有边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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