2013-07-05 19 views

回答

1

子類的UIButton和擴展的setEnabled:方法似乎工作:

- (void) setEnabled:(BOOL)enabled {  
    [super setEnabled:enabled]; 
    if (enabled) { 
     self.imageView.alpha = 1; 
    } else { 
     self.imageView.alpha = .25; 
    } 
} 
+0

這實際上爲工作您?在iOS 7中,我似乎無法改變按鈕圖像的Alpha設置。 – Axeva

0

你只是使用的背景素色?或者你在使用圖片嗎?

如果您使用的是圖像,那麼你可以如果你使用一種顏色,那麼你可以設置顏色的Alpha使用圖像內置了阿爾法。

但是,更改任何視圖的alpha會影響所有子視圖。

0

使用UIButton,您可以通過將按鈕樣式設置爲「自定義」而不是「Round Rect」來刪除背景。這使標題保持不變,但刪除了按鈕背景。如果您在Storyboard中執行此操作,則可以更改元素屬性中的按鈕樣式。對於代碼,格式是:

UIButton *button = [[UIButton alloc] buttonWithType:UIButtonTypeCustom]; 
// Setup frame and add to view 
21

這個工作對我來說:

self.myButton.backgroundColor = [UIColor clearColor]; 

,或者如果你不想把它徹底清除,但仍與透明度,你可以使用:

self.myButton.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:0.5]; 

第二個例子會給你alpha 0.5灰色。

+0

爲什麼我不能想到這個?謝謝! – Elsint

7

斯威夫特

import UIKit 

class SignInUpMenuTableViewControllerBigButton: UIButton { 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.applyCustomDesign() 
    } 

    func applyCustomDesign() { 
     // We set the background color of the UIButton. 
     self.clearHighlighted() 
    } 

    override var highlighted: Bool { 
     didSet { 
      if highlighted { 
       self.highlightBtn() 
      } else { 
       self.clearHighlighted() 
      } 
     } 
    } 

    func highlightBtn() { 
     self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0) 
    } 

    func clearHighlighted() { 
     self.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.05) 
    } 

} 
+0

加上一個'colorWithAlphaCompnent'!對於Swift 3:'UIColor.black.withAlphaComponent(0.8)' – kakubei

13

您還可以更改故事板顏色的α,

見附件圖片:

change alpha of color in storyboard/interface builder