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

标签 ios swift uitextfield

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

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

我用过:

self.userEmailTextField.backgroundColor = UIColor.clearColor()

占位符文本消失,但在使用 whiteColorblueColor 背景时出现。

白色背景:

userEmailTextField with .whiteColor Background

清晰的背景:

userEmailTextField with .clearColor Background

最佳答案

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

在 swift ...

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

在目标语言中...

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

关于ios - 将 UITextField 背景设置为 .clearColor 时,为什么我的 UITextField 占位符会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34647627/

相关文章:

ios - 使用 addUIInterruptionMonitor() 清除 XCUITest 中的 SFAuthenticationSession 警报

swift - 更新 Firebase 时由于未捕获的异常 'NSUnknownKeyException' 而终止应用程序

iphone - UITableViewCell 和 resignFirstResponder

iphone - 将自定义 UITableViewCell 中的 UITextField 移动到屏幕中央,这样键盘就不会覆盖它

ios - 在文本字段点击时显示日期选择器

iOS - 为版本 1.0 应用程序提交构建 1.7

ios - 限制删除 UITextField

objective-c - 是否可以在核心数据中使用自定义 sqlite 函数?

ios - 尝试呈现已经呈现

swift - 在以下示例中如何计算日差