我想创建一个带有自定义图像的uibarbuttonite,但我不想要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个视图中重复这个过程,这是不干的.

我想我必须细分一下,但是什么呢?

  • NSView?
  • UIBarButtonItem?

谢谢,

向您致敬,

推荐答案

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

@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

因此,您可以在代码中的任何位置创建调用此方法的栏项目(前提是在其声明中包含一个标头).

备注:您不需要使用"v"UIView,因为您可以直接使用按钮创建UIBarButtonItem作为自定义视图.
附注:您的代码中还需要[Forward Release].

Ios相关问答推荐

iOS中的分段拾取器—手柄点击已 Select 的项目

我想在expo 中使用useSafeAreaInsets来获取paddingbottom,但我得到了一个错误

SwiftUI拖动手势和内容拖动时的奇怪行为( skip /卡顿)

如何仅在TextField中的文本发生更改时才执行代码?

与iPadOS中带有扣件的模式相似的组件是什么?

一堆UIGraphics方法在iOS 17中被弃用,没有明确的替代方法?

如何在Swift中同时实现三个手势?

Flutter应用内购买无法恢复购买

为什么在Actor内部使用withTaskGroupwork并行运行?

在 iOS 上启用明文本地流量在 .NET MAUI 中不起作用

SwiftUI - TextField - 在焦点上清除 Double 类型的值,在取消 Select 时恢复

在 Swift 中映射 MySQL 数据

如何在 SwiftUI 的 TabView 中添加底部曲线?

-1103 错误域=NSURLErrorDomain 代码=-1103资源超过最大大小iOS 13

如何快速设置条形按钮的图像?

未找到签名证书​​iOS Distribution

如何以编程方式调用视图控制器?

如何比较两个 UIImage 对象

在 Swift 中禁用向后滑动手势

如何在键盘上方添加工具栏?