2011-05-06 17 views
1

禁用按鈕時。不透明度降低到50%。是有什麼辦法可以降低不透明度爲25%Disabale UIButton

+9

禁用後自行設置alpha值:'myButton.alpha = 0.25;'。 – 2011-05-06 11:44:39

+0

更確切地說'myButton.alpha = 0.5;'因爲它已經有50%的不透明度被禁用了,不是嗎? – 2011-05-06 12:38:36

回答

4

我將繼承的UIButton並重寫的setEnabled:方法是這樣的:

- (void) setEnabled:(BOOL)enabled { 
    NSLog(@"Button enabled = %d", enabled); 
    [super setEnabled:enabled]; 


    UIColor *color = self.backgroundColor; 
    if (!self.isEnabled) { 
     self.backgroundColor = [color colorWithAlphaComponent:0.75]; 
    } else { 
     self.backgroundColor = [color colorWithAlphaComponent:1.0]; 
    } 
} 
0

傑克·考克斯的回答迅速版:

override var enabled: Bool{ 
    didSet { 
     if enabled { 
      self.backgroundColor = self.backgroundColor?.colorWithAlphaComponent(1) 
     } else { 
      self.backgroundColor = self.backgroundColor?.colorWithAlphaComponent(0.75) 
     } 
    } 
}