Home > AI > IOS > SwiftUI >

Rectangle

Methods:

.fill()

Example : create a linear gradient rectangle


import SwiftUI


struct ContentView: View {
    var body:  some View {
        Image(systemName: "pencil")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .frame(minWidth: 0, maxWidth: .infinity)
            .overlay(TextOverlay(name: "Yellow Stone", park: "Center Park"))
    }
}



struct TextOverlay: View {
    var name: String
    var park: String

    var gradient: LinearGradient {
        LinearGradient(
            gradient: Gradient(
                colors: [Color.black.opacity(0.6), Color.black.opacity(0)]),
            startPoint: .bottom,
            endPoint: .center)
    }

    var body: some View {
        ZStack(alignment: .bottomLeading) {
            Rectangle()
                .fill(gradient)
            
            VStack(alignment: .leading) {
                Text(name)
                    .font(.title)
                    .bold()
                Text(park)
            }
            .padding()
        }
        .foregroundColor(.white)
    }
}

Leave a Reply