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

20m*_*man 2 uitextfield ios swift

使用 Xcode v7.2 - iOS v9.2 - iPhone5S Swift

我正在尝试创建一个UITextField带有浅灰色占位符文本的透明。

我用过了:

self.userEmailTextField.backgroundColor = UIColor.clearColor()
Run Code Online (Sandbox Code Playgroud)

占位符文本会消失,但在使用whiteColor、 或blueColor背景时会出现。

白色背景:

带有 .whiteColor 背景的 userEmailTextField

清晰的背景:

带有 .clearColor 背景的 userEmailTextField

Bho*_*ani 6

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

在斯威夫特...

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

在客观...

[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)


Kri*_*mar 5

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