使用colorWithPatternImage时如何保持图像透明度:

时间:2010-11-17 21:33:10

标签: iphone objective-c uiview

我有一张透明的图像,如下所示:

alt text

使用两个UI视图,我将背景颜色配置为:

self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.dashedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"red_square.png"]];

我希望红色正方形在UIView上重复保留透明度,但它是用这样的纯色填充的:

alt text

我不明白为什么。是否有一种简单的方法来绘制具有透明度的平铺图像?或者我是否需要查看绘制核心图形模式?

3 个答案:

答案 0 :(得分:25)

图案图像应该保持透明度。

尝试[self.dashedView setOpaque:NO]

答案 1 :(得分:5)

设置colorWithPatternImage后,您只需将opaque设置为NO。

self.dashedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"red_square.png"]];
self.dashedView.opaque = NO;

答案 2 :(得分:3)

Yar,感谢您的反馈。

我发现这只发生在iOS 4.3.x上,但不发生在iOS 5.0.x上。所以在4.3.x我必须做Yar id并将opaque设置为NO,然后设置背景图像,然后设置为YES。

UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.opaque = YES; // fix for strange issue in 4.x, need to set YES, set bg color image, then set back to NO
cancelButton.backgroundColor = [UIColor colorWithPatternImage: cancelImage];
cancelButton.opaque = NO;
相关问题