具有自定义图像且无边框的 UIBarButtonItem
我想创建一个带有自定义图像的 UIBarButtonItem,但我不想要 iPhone 添加的边框,因为我的图像有一个特殊的边框。
它与后退按钮相同,但前进按钮。
这个应用程序是针对内部项目的,所以我不在乎苹果是否拒绝或批准它或喜欢它:-)
如果我使用 UIBarButtonItem 的 initWithCustomView:v 属性,我可以做到这一点:
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。
我想我必须子类化,但是什么呢?
- NSView ?
- UIBarButtonItem ?
谢谢,
问候,
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.
This App is for an inHouse project, so I don't care if Apple reject or approves it or likes it :-)
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];
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 ?
thanks,
regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
另一个简单的解决方案是
Another simple solution is
您可以向 UIBarButtonItem 添加方法,而无需使用自定义类别对其进行子类化:
因此,您可以在代码中的任何位置创建调用此方法的栏项(前提是在其声明中包含标头)。
PS 您不需要使用 'v' UIView,因为您可以直接创建带有按钮的
UIBarButtonItem
作为自定义视图。PPS 您的代码中还需要 [forward release]。
You can add a method to UIBarButtonItem without subclassing it using custom category:
So anywhere in your code you can create bar item calling this method (provided that you include a header with its declaration).
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.
我发现这很容易。建议放在上面。 “random.png”必须在项目中。只需拖放任何图像即可。
I found it this ways easy. It is sugested on top. "random.png" has to be in project. Just drag and drop any image.
另一种方法是子类化 UIBarButtonItem。为什么?以便通过正确的发送者在目标上调用该操作。在上面的代码中,操作消息中的 sender 参数是 UIButton 实例,而不是 UIBarButtonItem 实例。例如,如果您希望从栏按钮项目中呈现 UIPopoverController,这将很重要。通过子类化 UIBarButtonItem,您可以添加一个保留原始目标的 ivar,从而允许我们的子类实例拦截、修改并向正确的发送者转发操作消息。
所以,CCFBarButtonItem.h:
和CCFBarButtonItem.m
An alternative is to subclass UIBarButtonItem. Why? So that the action is invoked on the target with the correct sender. In the code above, the sender argument in the action message is the UIButton instance, not the UIBarButtonItem instance. This would be important, for example, if you wish to present a UIPopoverController from the bar button item. By subclassing UIBarButtonItem, you can add an ivar that retains the original target, allowing our subclass instances to intercept, modify, and forward the action message with the proper sender.
So, CCFBarButtonItem.h:
and CCFBarButtonItem.m
这也可以通过编程来完成(当然):
首先,创建一个自定义视图。此自定义视图可以包含图像、按钮或您想要的任何其他内容。自定义视图可以通过编程方式或在 IB 中创建:
接下来,创建一个 UIBarButtonItem 并使用自定义视图对其进行初始化。
现在,只需将自定义 UIBarButton 添加到 leftBarButtonItem:
This can also be done programmatically as well (of-course):
First, create a custom view. This custom view can contain an image, button or whatever else you would like. The custom view can be made programmatically or in IB:
Next, create a UIBarButtonItem and initialize it with the custom view.
Now, just add the custom UIBarButton to the leftBarButtonItem:
好吧,这个类别工作得很好,因为 Popovercontroller 没有问题:-)
Ok that category works very good because there are no problems with Popovercontroller :-)
另一种解决方案,认为在以编程方式创建按钮时更简单:
One another solution, think it's simpler in case when creating button programatically:
检查这个简单的解决方案。
这里 1x1.png 是一个 1 像素透明 png 图像,您可以从下面的链接下载:
http: //commons.wikimedia.org/wiki/File:1x1.png
Check this out simple solution.
Here 1x1.png is a 1 pixel transparent png image which you can download from the link below
http://commons.wikimedia.org/wiki/File:1x1.png