【问题标题】:UIBarButton with CustomView and a Border带有 CustomView 和边框的 UIBarButton
【发布时间】:2012-04-04 11:52:37
【问题描述】:

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

这是我的代码(来自我的 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 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;

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

【问题讨论】:

    标签: iphone ios uibutton uibarbuttonitem


    【解决方案1】:

    在iOS5中,有一个技巧可以将动画图像放入UIBarButtonItem并保持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”等等:

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

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

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-27
          • 2013-10-14
          • 1970-01-01
          • 2013-09-18
          • 1970-01-01
          相关资源
          最近更新 更多