How do I change the background color of a UIView

Asked on 2024-07-30

1 search

To change the background color of a UIView in Swift, you can use the backgroundColor property. Here is a simple example:

let myView = UIView()
myView.backgroundColor = UIColor.red

If you are working with SwiftUI, you can use the background modifier to change the background color of a view. Here is an example:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .background(Color.red)
    }
}

For more advanced color treatments and visual effects in SwiftUI, you might find the session Create custom visual effects with SwiftUI particularly useful. This session covers various tools for bringing color into your app, including support for different gradient types, color controls, blend modes, and more.