以编程方式设置文本时,UITextField 占位符在 UITextField.text 后面显示有阴影。为什么?

UITextField placeholder showing up with a shadow behind UITextField.text when setting text programatically. Why?

我正在设置 UITextField 占位符的文本使用:

textField.placeholder = "placeholder"

Then, as I am using a UIPickerView as my inputView, when a picker view element is selected I update the placeholder by doing textField.placeholder = nil and making the textField.text = "pickerViewValue"。但是占位符不清晰,文本后面有轻微的阴影,如下所示:

https://imgur.com/a/sMHvEje

(你必须看得很近才能看到它。你会看到它在较暗的 L 后面非常隐约地写着 'Size')。

我尝试将文本和 attributedText 设置为占位符而不是使用占位符 属性,但每当我这样做时,第一个值设置为文本或 attributedText就像占位符文本一样保留在背景中。我已经尝试将所有这些属性设置为 nil 和“​​”。没有骰子。想知道是否有人遇到过类似的问题。

在格式化代码时遇到问题,所以这是一个 pastebin:

https://pastebin.com/pqmtaBeG

重要的东西在 ProductView

的配置、updatePickerViews 和 extensions
let toolBar = UIToolbar()
  toolBar.barStyle = .default
  toolBar.isTranslucent = true
  toolBar.tintColor = globalAppleBlue
  toolBar.sizeToFit()

  let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)

  let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(donePickerViewButtonTapped(_:)))
  doneButton.setTitleTextAttributes([
    NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18)], for: .normal)

  toolBar.setItems([spaceButton, doneButton], animated: false)
  toolBar.isUserInteractionEnabled = true

  let textField = VoraTextField(leftPadding: globalPadding * 1.5, rightPadding: 0.0)
  textField.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 0.9)

  textField.placeholder = option.name.capitalized

  let imageView = UIImageView(image: UIImage(named: "right-angle"))
  imageView.contentMode = .scaleAspectFit

  textField.rightView = imageView
  textField.rightViewMode = .always
  textField.font = UIFont.boldSystemFont(ofSize: 16)
  textField.textColor = .black
  textField.delegate = self

  textField.inputAccessoryView = toolBar

  let pickerView = UIPickerView()
  self.pickerViewArray.append(pickerView)
  pickerView.showsSelectionIndicator = true
  pickerView.backgroundColor = .white
  textField.inputView = pickerView
  textField.borderStyle = .roundedRect

  pickerView.delegate = self

我不太确定是什么导致了这个问题,但作为一种解决方法,请尝试在您的文本字段中添加纯色背景。

UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1) // changed alpha to 1

当然,使用该值,颜色会更深。