根据是否是第一响应者设置文本视图的背景颜色

Set background color of text view based on whether it is the first responder

如何根据是否是第一响应者设置文本视图的背景颜色?

似乎已经回答了这个问题的变体,但它在 Objective C 中。我希望有一个 swift 等价物可以不断检查文本视图的状态。这是我的代码:

if messageInputBar.inputTextView.isFirstResponder {
         messageInputBar.inputTextView.backgroundColor = .clear
 } else {
         messageInputBar.inputTextView.backgroundColor = .white
 }

实现 textViewDidBeginEditingtextViewDidEndEditing 委托方法。根据需要更新文本视图的 backgroundColor

func textViewDidBeginEditing(_ textView: UITextView) {
    textView.backgroundColor = .blue
}

func textViewDidEndEditing(_ textView: UITextView) {
    textView.backgroundColor = .white
}

并且不要忘记设置每个文本视图的 delegate