UITextView внутри SwiftUI просмотра фона и цвета рамки для темного режима (Форма) - PullRequest
3 голосов
/ 05 февраля 2020

Я пытаюсь заставить UITextView выглядеть так же, как (SwiftUI) TextField, код UITextView выглядит следующим образом:

...
func makeUIView(context: UIViewRepresentableContext<TextView>) -> UITextView {
  let textView = UITextView()

  textView.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body) 
  textView.text = placeholderText
  textView.textColor = .placeholderText
  textView.backgroundColor = .systemBackground
  textView.layer.borderColor = UIColor.placeholderText.cgColor
  textView.layer.borderWidth = 1
  textView.layer.cornerRadius = 6

  return textView
}
...

Полный код похож на this

В форме это выглядит нормально в светлом режиме: enter image description here

Однако в форме в темном режиме:

enter image description here

Что я делаю не так:

textView.backgroundColor = .systemBackground
textView.layer.borderColor = UIColor.placeholderText.cgColor

1 Ответ

0 голосов
/ 05 февраля 2020

Ах, я понял это. UITextView, который у меня есть, находится в UIViewController (для лучшего контроля размера)

Серый sh фон не отображается с этой настройкой.

public class CustomTextView: UIViewController {
    var textView: UITextView!

    init() {
        super.init(nibName: nil, bundle: nil)
        self.textView = UITextView(frame: .zero)
        self.textView.frame = self.view.bounds
        self.view.addSubview(textView)
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        textView = UITextView(frame: .zero)
        textView.frame = self.view.bounds
        self.view.addSubview(textView)
    }
}

Тогда в вашем UIViewRepresentable

public func makeUIViewController(context: UIViewControllerRepresentableContext<TextViewController_UI>) -> TextViewController_UI.CustomTextView {
    let textViewCont = CustomTextView()

    textViewCont.textView.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
    textViewCont.textView.text = "placeholder"
    textViewCont.textView.textColor = .placeholderText
    textViewCont.textView.backgroundColor = .systemBackground
    textViewCont.textView.layer.borderColor = UIColor.placeholderText.cgColor
    textViewCont.textView.layer.borderWidth = 1
    textViewCont.textView.layer.cornerRadius = 6
    return textViewCont
}
...