带UIImage的UIToolbar按钮不完全透明 [英] UIToolbar buttons with UIImage not entirely transparent

查看:98
本文介绍了带UIImage的UIToolbar按钮不完全透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经永远拥有了这个代码,它曾经工作过,但它开始在iOS 6中表现出恼人的行为。在SO上有几个类似的线程,但我还没有找到解决方案我觉得很舒服。我找到了解决办法(见下文),但这似乎不是正确的做法。



,jd的回答提供了解决方法(编辑以匹配我的变量):

  const float colorMask [6] = {222,255,222,255,222,255}; 
UIImage * img = [[UIImage alloc] init];
UIImage * maskedImage = [UIImage imageWithCGImage:CGImageCreateWithMaskingColors(img.CGImage,colorMask)];

[toolBarButtons setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

我也看到了这个问题的几个解决方案,需要获得顶部或底部的颜色 navigationController 并应用渐变,但我拒绝相信无法制作 UIToolbar 完全透明,应该允许 navigationController 颜色通过,渐变和所有。



应用掩码图片 真的 是模拟正确UI体验的唯一方法吗? Apple 真的 没有提供简单地让包含矩形的 UIToolbar 完全透明的方法吗?



目前,我有正确的行为,但我觉得我没有正确的解决方案。这感觉就像一个黑客。有没有人知道更好的方法来实现这个目标?

解决方案

我在我的应用程序中做同样的事情。 (这让我很震惊,我们必须在 UINavigationBar 中托管 UIToolbar ,因为 UINavigationBar 不会像 UIToolbar 一样呈现 UIBarButtonItem的。)



以下是我为避免您遇到的问题所做的工作:

 工具栏.barStyle = self.navigationController.navigationBar.barStyle; 
toolbar.tintColor = self.navigationController.navigationBar.tintColor;

我最后一次检查时,navigationBar.barStyle是一个未以其他方式记录在UIBarStyle枚举...我认为它是'3'。



编辑:令人讨厌的是,这不适用于iPhone,因为它适用于iPad ...



这个UIToolbar子类似乎工作正常。使用它代替普通的UIToolbar:

  @interface NavToolbar:UIToolbar 
@end

@implementation NavToolbar
- (void)layoutSubviews
{
[super layoutSubviews];
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
}
- (void)drawRect:(CGRect)rect
{
}
@end

而且,这是用来演示使用它的代码。如果没有NavToolbar,我会看到你提出的问题;使用NavToolbar可以根据需要使用它。在iOS6手机模拟器中测试。

   - (void)viewDidLoad 
{
[super viewDidLoad];

UIToolbar * tb = [[NavToolbar alloc] initWithFrame:CGRectMake(0,0,44,44)];

UIBarButtonItem * bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];

tb.items = @ [bbi];
tb.barStyle = self.navigationController.navigationBar.barStyle;

UIBarButtonItem * tbbbi = [[UIBarButtonItem alloc] initWithCustomView:tb];
tbbbi.width = 44;

self.navigationItem.rightBarButtonItem = tbbbi;
}


I've had this code forever, and it used to work, but it started exhibiting an annoying behavior in, I think, iOS 6. There are several similar threads on SO, but I haven't found a solution that I am comfortable with as of yet. I found a work-around (see below), but it doesn't seem like the "right" way to do it.

Here's a thread with the same issue, but no answers.

When I use an image as a UIBarButtonItem and put it into a UIToolbar, the containing rectangle of the UIToolbar is not entirely transparent. The PNG images that I use for the UIBarButtonItem are transparent, so that's not the issue. Here is an example:

When I use text instead of an image as the UIBarButtonItem, transparency is working as expected. As so:

Here is the code that has worked since forever:

NSMutableArray *buttonItems = [[[NSMutableArray alloc] initWithCapacity: 1] autorelease];
UIImage *printButtonImage = [UIImage imageNamed:@"buttonPrint.png"];
UIToolbar *toolBarButtons = [[[UIToolbar alloc] initWithFrame:CGRectMake( 0, 0, 52.0, 44.01 )] autorelease];
UIBarButtonItem *printButton = [[[UIBarButtonItem alloc] initWithImage:printButtonImage style:UIBarButtonItemStyleBordered target:self action:@selector(printDocument:)] autorelease];
[buttonItems addObject:printButton];

[toolBarButtons setItems:buttonItems animated:YES];

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:toolBarButtons] autorelease];

According to suggestions from SO, I have added this just prior to [toolBarButtons setItems...] but it has had no affect:

toolBarButtons.backgroundColor = [UIColor clearColor];
[toolBarButtons setTranslucent:YES];
[toolBarButtons setOpaque:NO];

I found this thread, on which the answer by jd provided a work-around (edited to match my variables):

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

[toolBarButtons setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

I have also seen a couple of solutions to this issue that require getting the color of the top or bottom of the navigationController and applying a gradient, but I refuse to believe that there is no way to make the rectangle of the UIToolbar entirely transparent, which should allow the navigationController color to come through, gradient and all.

Is applying a mask image really the only way to simulate the correct UI experience? Has Apple really not provided a way to simply make a UIToolbar containing rectangle entirely transparent?

For now, I have the correct behavior, but I don't feel like I have the correct solution. This feels like a hack. Does anyone out there know of a better way to accomplish this?

解决方案

I do this same thing in my app. (It drives me nuts that we have to host a UIToolbar in a UINavigationBar simply because UINavigationBar doesn't render UIBarButtonItem's in the same way that UIToolbar does.)

Here's what I do to avoid the problem you're seeing:

toolbar.barStyle = self.navigationController.navigationBar.barStyle;
toolbar.tintColor = self.navigationController.navigationBar.tintColor;

The last time I checked, the navigationBar.barStyle is a value that isn't otherwise documented in a UIBarStyle enumeration... I think it was '3'.

Edit: Annoyingly, this doesn't work on iPhone as it works on iPad...

This UIToolbar subclass seems to work fine. Use it instead of a plain UIToolbar:

@interface NavToolbar : UIToolbar
@end

@implementation NavToolbar
- (void) layoutSubviews
{
    [super layoutSubviews];
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
}
- (void) drawRect:(CGRect)rect
{
}
@end

And, here's code to demonstrate using it. Without the NavToolbar I see the issue you've presented; with the NavToolbar it works as desired. Tested in the iOS6 Phone simulator.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIToolbar* tb = [[NavToolbar alloc] initWithFrame: CGRectMake(0, 0, 44, 44)];

    UIBarButtonItem* bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target: nil action: nil];

    tb.items = @[bbi];
    tb.barStyle = self.navigationController.navigationBar.barStyle;

    UIBarButtonItem* tbbbi = [[UIBarButtonItem alloc] initWithCustomView: tb];
    tbbbi.width = 44;

    self.navigationItem.rightBarButtonItem = tbbbi;
}

这篇关于带UIImage的UIToolbar按钮不完全透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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