OGeek|极客世界-中国程序员成长平台

标题: iphone - 带有 CustomView 和边框的 UIBarButton [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:52
标题: iphone - 带有 CustomView 和边框的 UIBarButton

我已将 UIBarButtonItem 子类化,并试图制作一个正常显示刷新图像的按钮,但在加载时是一个事件微调器。我遇到的问题是我无法让边框样式在里面显示自定义 View 。它只是没有出现。

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

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 actionselector(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 actionselector(didTapAbortButton forControlEvents:UIControlEventTouchUpInside];
[viwInner addSubview:self.btnLoading];

return self;

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



Best Answer-推荐答案


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

UIImage *image = [UIImage animatedImageNamed"refresh-" duration:1.f];
self.button = [[UIBarButtonItem alloc] initWithImage:image
                                               style:UIBarButtonItemStyleBordered 
                                              target:self
                                              actionselector(doSomething];

然后,创建一组图像,动画的每一帧一个图像,然后命名为“refresh-0.png”、“refresh-1.png”等等:

refresh-0.png refresh-1.png refresh-2.png

当你想停止动画时,将按钮的图像替换为静态版本:

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

自己创建所有这些图像仍然很麻烦,但它可能比创建自己的按钮边框背景更一致。

关于iphone - 带有 CustomView 和边框的 UIBarButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10010534/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4