in iOS 16 beta 4, the following code ignores the UITextView.appearance().backgroundColor = .clear
struct ContentView: View {
@State var text = "Type something here."
var body: some View {
ZStack {
Color.blue.ignoresSafeArea()
TextEditor(text: $text)
.frame(height: 100)
.padding([.leading, .trailing], 40)
}
.onAppear {
UITextView.appearance().backgroundColor = .clear
}
}
}
Earlier, I had assumed it was Xcode 14 beta 4 - but further investigation shows that it's the new iOS beta.
I would obviously prefer that TextEditor correctly handle it's own .background() method, but in the meantime, any ideas for a workaround?
Work-around found!
I found this in the release notes:
Added View.scrollContentBackground, which permits customization of background colors for scrollable views like List. (45928055)
And so here’s the new version…
struct ContentView: View {
@State var text = "Type something here."
var body: some View {
ZStack {
Color.blue.ignoresSafeArea()
if #available(iOS 16.0, *) {
TextEditor(text:$text)
.multilineTextAlignment(.center)
.scrollContentBackground(Visibility.hidden) // new technique for iOS 16
.frame(height: 100)
.padding([.leading, .trailing], 40)
} else {
// Fallback on earlier versions
TextEditor(text:$text)
.multilineTextAlignment(.center)
.frame(height: 100)
.padding([.leading, .trailing], 40)
}
}
.onAppear {
// Fallback on earlier versions
UITextView.appearance().backgroundColor = .clear
}
}
}