【问题标题】:UITextField placeholder showing up with a shadow behind UITextField.text when setting text programatically. Why?以编程方式设置文本时,UITextField 占位符在 UI​​TextField.text 后面显示阴影。为什么?
【发布时间】:2019-09-19 16:38:00
【问题描述】:

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

textField.placeholder = "placeholder"

然后,当我使用UIPickerView作为我的inputView时,选择拾取器视图元素时,通过DOPD textField.placeholder = nil并制作textField.text = "pickerViewValue",更新placeholder。但是占位符不清晰,文字后面有轻微的阴影如下图:

https://imgur.com/a/sMHvEje

(您必须非常靠近才能看到它。您会看到它在较暗的 L 后面非常隐约地显示“Size”)。

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

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

https://pastebin.com/pqmtaBeG

重要的东西在ProductView的configure、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

【问题讨论】:

  • 请提供代码sn-ps
  • 更新了!请让我知道我是否可以添加任何其他有用的内容。

标签: ios swift uitextfield uitextfielddelegate inputview


【解决方案1】:

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

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

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

【讨论】:

  • 这解决了我的问题,因此我将其标记为已接受的答案。如果您认为可以找出根本问题,请随意添加更多答案。感谢您的快速响应埃尔伯特!
猜你喜欢
  • 2016-08-27
  • 2015-11-13
  • 2013-02-12
  • 2011-11-11
  • 2016-03-14
  • 1970-01-01
  • 2019-02-15
  • 2011-04-09
  • 2017-09-16
相关资源
最近更新 更多