August 9, 2022Change TextEditor background in SwiftUI
Starting from iOS 16 we can set a custom background for TextEditor in SwiftUI using a combination of scrollContentBackground() and background() view modifiers. We first have to hide the default background on TextEditor
by applying scrollContentBackground(.hidden)
, otherwise our custom background won't be visible. Then we can easily set a new background with the background()
method.
struct ContentView: View {
@State private var text = "Some text"
var body: some View {
TextEditor(text: $text)
.frame(width: 300, height: 200)
.scrollContentBackground(.hidden)
.background(.indigo)
}
}
Level up your Swift skills!$35
100+ tips to take your Swift code to the next level
Swift Gemsby Natalia Panferova
- Advanced Swift techniques for experienced developers bypassing basic tutorials
- Curated, actionable tips ready for immediate integration into any Swift project
- Strategies to improve code quality, structure, and performance across all platforms
Level up your Swift skills!
100+ tips to take your Swift code to the next level
Swift Gems
by Natalia Panferova
$35If you are enjoying our blog and would like to support us, you can sponsor us on GitHub.
Subscribe to our monthly newsletter to get exclusive Swift and SwiftUI tips, project updates, behind-the-scenes insights, and special discounts on our books.
For more frequent updates, follow us on X @nilcoalescing.