2015-04-21 58 views

回答

8

您正在尋找這樣的事情:

CGFloat newAlpha = 0.7; 
CGColorRef color = CGColorCreateCopyWithAlpha(self.yourTextField.layer.backgroundColor, newAlpha); 
[self.yourTextField.layer setBackgroundColor:color]; 

或者,如果你已經知道的顏色,只是希望當前阿爾法:

CGFloat alpha = CGColorGetAlpha(self.yourTextField.layer.backgroundColor); //Get current alpha  
self.yourTextField.layer.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:alpha].CGColor; 
+0

你能否更新這個以包含UIColor和CGColor? –

9

您可以使用:

UIColor *color = ...; 
color = [color colorWithAlphaComponent:0.5f]; 
2

In Swift 3 or 4:

let color: UIColor = UIColor.black.withAlphaComponent(0.7)