背景色默認值引出的一系列問題

剛看到代碼中有大量的xx.backgroundColor = [UIColor clearColor];html

(1)很好奇,默認的背景色是什麼值?ios

官方給出的:默認爲nil,此時背景是透明的。而後clearColor的備註也寫明瞭:「0.0 white, 0.0 alpha」(白色,但alpha爲0),因此我感受代碼中設置clearColor的代碼能夠去掉,除非此舉是爲了reset背景色。app

(2)結束了這個問題後,想到了UIView有個屬性是opaque,若是此屬性設置爲YES,也就是不透明,可是默認背景色是透明的,那。。。。會發生什麼?ide

opaque屬性默認爲YES,這點本身沒有理解透徹,若是是YES,那爲何疊加的View會徹底被看到呢?跟背景色的默認值是否是衝突了呢?翻看官方文檔後發現:this

An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.spa

opaque爲YES的視圖的內容區域必須不透明:內容的alpha值須要爲1。若是設置了opaque屬性爲YES,但內容中有部分透明或者所有透明,結果會是沒法預期的。這樣最好仍是要設置爲NO。.net

You only need to set a value for the opaque property for subclasses of UIView that draw their own content using the drawRect: method. The opaque property has no effect for system provided classes such as UIButton, UILabel, UITableViewCell, etc.htm

opaque屬性只須要對繼承自UIView的、使用drawRect:進行繪製內容的類(自定義類)中使用,對系統提供的類,例如UIButtonUILabelUITableViewCell,等,使用此屬性,是沒有效果的。對象

這下明白了,這個屬性更可能是爲drawRect:方法繪製出內容的對象準備的,若是更多的是對系統提供控件的使用,通常狀況下咱們用不到。blog

 

而後我又看到了一篇相關文章,順便延伸一下:

原文地址:http://blog.csdn.net/wzzvictory/article/details/10076323

文章講述了hidden、alpha、opaque的區別,其中說到了:

alpha爲0和hidden爲YES時,當前UIView會從響應者鏈中移除,而響應者鏈中的下一個會成爲第一響應者,這一點比較有用

做者還提到了,alpha設置爲0和hidden爲YES有什麼區別,我知道的是,在Windows Phone開發中,前者是須要CPU或GPU繪製的,然後者是不須要的,在iOS上是否如此,也但願你們不吝賜教!

相關文章
相關標籤/搜索