import SwiftUI
struct OverlayBackgroundDemo: View {
var body: some View {
Rectangle()
.fill(Color.blue)
.frame(width: 200, height: 100)
.overlay(
Text("Hello SwiftUI")
.foregroundColor(.white)
.font(.headline)
)
.overlay(
Circle()
.fill(Color.red)
.frame(width: 30, height: 30)
.offset(x: 85, y: -35), alignment: .topTrailing
)
.background(
RoundedRectangle(cornerRadius: 15)
.fill(Color(white: 0.9))
.frame(width: 220, height: 120)
)
}
}
struct OverlayBackgroundDemo_Previews: PreviewProvider {
static var previews: some View {
OverlayBackgroundDemo()
}
}We start with a blue rectangle sized 200x100 points. We add the white text "Hello SwiftUI" centered on top using the overlay modifier. Then, we add a small red circle as another overlay aligned to the top-right corner by using alignment: .topTrailing and offsetting it slightly to position it nicely. Finally, we add a light gray rounded rectangle as the background behind the blue rectangle using the background modifier with a corner radius of 15 and slightly larger size to frame the blue rectangle visually.
This layering shows how overlays and backgrounds can be stacked to create rich UI designs in SwiftUI.