将UITextField背景设置为.clearColor时,为什么我的UITextField占位符会消失?

时间:2016-01-07 04:54:23

标签: ios swift uitextfield

使用Xcode v7.2 - iOS v9.2 - iPhone5S 迅速

我正在尝试使用浅灰色占位符文本创建透明的UITextField

我用过:

self.userEmailTextField.backgroundColor = UIColor.clearColor()

占位符文字会消失,但在使用whiteColorblueColor背景时会显示。

白色背景:

userEmailTextField with .whiteColor Background

明确背景:

userEmailTextField with .clearColor Background

2 个答案:

答案 0 :(得分:4)

由于您的背景颜色清晰,因此看不到占位符。您可以通过设置这样的掌文颜色来检查它...

在斯威夫特......

if let _ = self.placeholder{
 self.txtField.attributedPlaceholder = NSAttributedString(string:self.placeholder!,
    attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()])
  }

在目标c ...

[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];

答案 1 :(得分:3)

占位符颜色根据文本字段的颜色而变化。因此,当您设置清晰颜色时,它也会相应地设置占位符颜色。基本上它不会消失但不可见。