2012-03-06 9 views
20

Tôi có một UIView với alpha của .5 Tôi đã thêm một subview với alpha của 1.View với alpha thấp - subview với alpha cao

Các subview dường như thừa hưởng giá trị alpha của công ty mẹ. Có cách nào để làm cho chế độ xem phụ mờ đục hơn chế độ xem gốc không?

mã trông như thế này:

CGRect promptFrame = CGRectMake(55, 80, 180, 50); 
UIView *inputPrompt = [[UIView alloc] initWithFrame: promptFrame]; 
[inputPrompt setBackgroundColor: [UIColor darkGrayColor]]; 
[inputPrompt setAlpha: .5]; 
inputPrompt.layer.cornerRadius = 8; 
inputPrompt.layer.masksToBounds = YES; 

CGRect fileTextFieldFrame = CGRectMake(10, 15, 150, 25); 
UITextField *filePrompt = [[UITextField alloc] initWithFrame: fileTextFieldFrame]; 
[filePrompt setBorderStyle:UITextBorderStyleRoundedRect]; 
[filePrompt setClearButtonMode:UITextFieldViewModeWhileEditing]; 
[filePrompt setBackgroundColor: [UIColor whiteColor]]; 
[filePrompt setAlpha: 1]; 

Kết quả trông như thế này: enter image description here

Tôi muốn để có thể thấy nút dưới màu xám UIView nhưng không dưới trắng UITextField. Làm thế nào để tôi làm điều này?

Trả lời

79

Đặt alpha của nền màu inputPrompt không phải alpha trực tiếp.

[inputPrompt setBackgroundColor:[[UIColor darkGrayColor] colorWithAlphaComponent:0.5]]; 
//[inputPrompt setAlpha: .5]; 
+1

Điều này có hiệu quả. Cảm ơn. – StoneBreaker

+1

+1 và cảm ơn rất nhiều .. :) – Sarah

+1

+1 hoạt động rất tốt .. –