Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 带有CustomView和边框的UIBarButton_Iphone_Ios_Uibutton_Uibarbuttonitem - Fatal编程技术网

Iphone 带有CustomView和边框的UIBarButton

Iphone 带有CustomView和边框的UIBarButton,iphone,ios,uibutton,uibarbuttonitem,Iphone,Ios,Uibutton,Uibarbuttonitem,我已经对UIBarButtonItem进行了子类化,并尝试创建一个按钮,该按钮通常显示刷新图像,但在加载时显示活动微调器。我遇到的问题是,我无法使边框样式在内部显示自定义视图。只是没有出现 这是我的代码(来自UIBarButtonItem子类的构造函数): 有没有这样做的原因?由于UIBarButtonims(使用-initWithImage:style:target:action:或-initWithTitle:style:target:action:创建)不支持按钮内的任意视图,因此无法执行

我已经对UIBarButtonItem进行了子类化,并尝试创建一个按钮,该按钮通常显示刷新图像,但在加载时显示活动微调器。我遇到的问题是,我无法使边框样式在内部显示自定义视图。只是没有出现

这是我的代码(来自UIBarButtonItem子类的构造函数):


有没有这样做的原因?

由于UIBarButtonims(使用
-initWithImage:style:target:action:
-initWithTitle:style:target:action:
创建)不支持按钮内的任意视图,因此无法执行您尝试执行的操作

您可以尝试将
UIActivityIndicatorView
放置在模拟按钮边框的图像顶部。然后可以使用
initWithCustomView:
将视图添加到按钮中


希望这能有所帮助。

在iOS5中,有一个技巧可以将动画图像放入
UIBarButtonItem
并保持
uiBarbuttonItemStyleBorded

UIImage *image = [UIImage animatedImageNamed:@"refresh-" duration:1.f];
self.button = [[UIBarButtonItem alloc] initWithImage:image
                                               style:UIBarButtonItemStyleBordered 
                                              target:self
                                              action:@selector(doSomething:)];
然后,创建一组图像,动画的每一帧一个图像,然后命名为“refresh-0.png”、“refresh-1.png”等等:

如果要停止动画,请将按钮的图像替换为静态版本:

self.button.image = [UIImage imageNamed:@"refresh-0.png"];

要自己创建所有这些图像仍然是一件非常麻烦的事情,但这可能比创建自己的按钮边框背景更加一致。

要使用UIActivityIndicatorView来完成此操作,而不是替换它,您必须自己渲染按钮边框。我要做的是将UIBarButtonim的customView设置为包含边框的UIImageView,并将活动视图添加为该图像的子视图

这就给您留下了获取边界图像的问题。如果你只需要一种颜色,那么你可以从模拟器屏幕截图中裁剪出来;我编写了一个Python脚本,如果您需要在多个条颜色上使用它,那么您不仅要获得边框像素,还要获得边框透明度

self.button.image = [UIImage imageNamed:@"refresh-0.png"];