2010-11-17 89 views
17

我与透明度准备的图像,像这样:如何使用colorWithPatternImage时保留图像的透明度:

alt text

有两个UIviews,我设置背景色为这样:

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

我希望重复在与透明度的UIView保留了红色正方形,但它已经通过了坚实的颜色像这样充满:

alt text

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

回答

25

图形图像应保持透明度就好了。

尝试[self.dashedView setOpaque:NO]

+0

完美,谢谢。我以前通过Interface Builder尝试过这种方式,但似乎并不奏效。但是,以编程方式设置它。 – msmithstubbs 2010-11-17 21:40:04

+0

这些选项通过IB有一些问题,我已经遇到过很多次。我认为它有潜在的初始化顺序问题。 – 2010-11-17 21:41:18

+0

对于后人,我发现有必要将图层设置为非不透明,例如:[self.dashedView.layer setOpaque:NO]。请参阅:http://stackoverflow.com/questions/4059487/iphone-uiviews-backgroundcolor-using-a-png-with-transparency – tba 2011-05-19 21:25:20

3

亚尔,感谢您的反馈意见。

我发现这只是发生在iOS 4.3.x版而不是在iOS 5.0.x中所以在4.3.x上,我必须做Yar id并将不透明设置为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; 
5

您只需在设置colorWithPatternImage后将不透明设置为NO即可。

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

事实上,4.3的重要之处在于在设置背景颜色(或UIImageView情况下的图像)后设置不透明度。 – 2012-05-18 18:40:53

相关问题