2011-08-24 108 views
8

我正在使用以編程方式從按鈕製作陰影的標準方式,但是我想在使用按鈕完成後不再存在陰影。我可以將不透明度設置爲0,但影子是否仍然存在,如果是這樣,它仍會對系統徵稅。感謝我如何刪除iPhone中的陰影

這給出了一個錯誤

tempButton.superview.layer.shadowOffset = nil; 
    tempButton.superview.layer.shadowRadius = nil; 
    tempButton.superview.layer.shadowOpacity = nil; 
+0

可能重複的[iOS添加/從視圖中刪除陰影](http://stackoverflow.com/questions/16751163/ios-add-remove-shadow-from-a-view) – Fattie

回答

26

我通常會做以下安全的事情。

[[tempButton layer] setShadowOpacity:0.0]; 
    [[tempButton layer] setShadowRadius:0.0]; 
    [[tempButton layer] setShadowColor:nil]; 

石英高度優化,不會浪費任何時間渲染,如果它不必。

+0

+1但我想知道是否'[[tempButton layer] setShadowColor:nil];'是非常安全的。該文件說默認是不透明的黑色,但沒有指定如何處理零(並依靠無證行爲從不安全)。我個人會跳過那一個。 – Clafou

+0

'nil'似乎工作正常,但如果你想玩它安全去用[UIColor clearColor] .CGColor。這工作得很好,很安全。 – Joel

1

我只想刪除的按鈕,並具有相同的(但非陰影)按鈕替換它。或者同時保持和隱藏/取消隱藏其中之一。有時候創建一個新的UI對象要比現有的對象更容易。