带有CustomView和边框的UIBarButton [英] UIBarButton with CustomView and a Border

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

问题描述

我将UIBarButtonItem子类化,并试图制作一个按钮,该按钮通常不会显示刷新图像,但是在加载时是一个活动微调器.我的问题是我无法使用边框样式显示内部的自定义视图.它只是没有出现.

I've subclassed UIBarButtonItem and am trying to make a button which dispays a refresh image normally, but an activity spinner when loading. The problem I have is I can't get the bordered style to display a custom view inside. It just doesn't appear.

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

This is my code (from my UIBarButtonItem subclass's constructor):

self = [super initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:nil];
UIView *viwInner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24,24)];
[self.customView addSubview:viwInner];

self.btnStandard = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnStandard setFrame:CGRectMake(0, 0, 24,24)];
UIImage *initialImage = [[UIImage imageNamed:@"refresh_24.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[self.btnStandard setBackgroundImage:initialImage forState:UIControlStateNormal];
[self.btnStandard setBackgroundImage:initialImage forState:UIControlStateHighlighted];
[self.btnStandard setBackgroundImage:initialImage forState:UIControlStateSelected];
[self.btnStandard addTarget:self action:@selector(didTapInitialButton:) forControlEvents:UIControlEventTouchUpInside];
[viwInner addSubview:self.btnStandard];

self.btnLoading = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnLoading setFrame:CGRectMake(0, 0, 24,24)];
self.loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActionSheetStyleBlackOpaque];
[self.loadingView setHidesWhenStopped:true];
[self.loadingView stopAnimating];
[self.btnLoading addSubview:self.loadingView];
[self.btnLoading addTarget:self action:@selector(didTapAbortButton:) forControlEvents:UIControlEventTouchUpInside];
[viwInner addSubview:self.btnLoading];

return self;

这有什么不起作用的原因吗?

Is there a reason this isn't working?

推荐答案

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

In iOS5, there is a trick to get an animated image into a UIBarButtonItem and maintain the UIBarButtonItemStyleBordered:

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",依此类推:

Then, create a set of images, one image for each frame of the animation, and name then "refresh-0.png", "refresh-1.png" and so forth:

要停止动画时,请用静态版本替换按钮的图像:

When you want to stop the animation, replace the image of the button with a static version:

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

必须亲自创建所有这些图像仍然很麻烦,但是它可能比创建自己的Button-border背景更加一致.

It's still a significant hassle having to create all these images yourself, but it's probably more consistent than creating your own Button-border background.

这篇关于带有CustomView和边框的UIBarButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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