2010-11-17 87 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

相關問題