[SwiftUI]TextEditorの背景色が変わらない時の対処法

SwiftUI

どうも、ちょげ(@chogetarou)です。

「background修飾子で色を設定したのに、TextEditorの背景色が変わらない!」
という人に向けて、その対処方法を紹介します。

スポンサーリンク

対処法

インターフェース, インターネット, プログラム, ブラウザ, Www

TextEditorの背景色を変わらない場合には、UITextViewを使います。

具体的には、initメソッドやonAppear修飾子で、UITextView.appearance().backgroundColorに「.clear」を代入します。

init() {
     UITextView.appearance().backgroundColor = .clear
}

あとは、background修飾子でTextEditorの背景色を設定します。

TextEditor(text: $editText)
    .background(背景色)

TextEditorの背景色が変わります。

使用例

struct ContentView: View {
    @State var editText = ""
    
    init() {
        UITextView.appearance().backgroundColor = .clear
    }
    var body: some View {
        VStack {
            TextEditor(text: $editText)
                .frame(width: 200, height: 100)
                .border(Color.black, width: 2)
                .background(Color.blue.opacity(0.2))
        }
    }
}

コメント

タイトルとURLをコピーしました