的UITextField上设置backgroundColor隐藏阴影

问题描述:

所以我定制我的UITextField.layer有阴影:的UITextField上设置backgroundColor隐藏阴影

[userNameField.layer setBorderColor: [[UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1.0] CGColor]]; 
[userNameField.layer setBorderWidth: 1.0]; 
[userNameField.layer setCornerRadius:6.0]; 
[userNameField.layer setShadowOpacity:0.7]; 
[userNameField.layer setShadowColor:[[UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1.0] CGColor]]; 
[userNameField.layer setShadowOffset:CGSizeMake(0.5, 0.5)]; 

这就像一个魅力,但背景是透明的。 现在,当我设置背景颜色:

[userNameField.layer setBackgroundColor:[[UIColor whiteColor] CGColor]]; 

的阴影覆盖(与背景颜色隐藏)。

有谁知道如何设置背景颜色和阴影文本字段?

+0

我有同样的问题是,当我试图在文本字段上设置阴影时,它不工作/显示,我没有意识到这是因为我已经设置了backgroundColor,所以对于OP来说+1! –

试试这个 textField.borderStyle = UITextBorderStyleRoundedRect;

你只需要明确设置UITextField层的masksToBounds属性没有像这样:

myTextField.layer.masksToBounds = NO; 

您可以设置保证金:

myTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 7, 7)]; 

试试这个