Is it possible to define a RoundedRectangle with some properties as a variable for later usage in SwiftUI?

I want to reuse a RoundedRectangle that has certain properties multiple times in my View. Is defining it as a variable possible?

And while my code is pretty much like this

Struct ContentView: View {
   var body: some View {
      
      RoundedRectangle(cornerRadius: 24)
                        .frame(width: 180, height: 240)
                        .foregroundColor(Color(.white))

      RoundedRectangle(cornerRadius: 24)
                        .frame(width: 180, height: 240)
                        .foregroundColor(Color(.white))

      RoundedRectangle(cornerRadius: 24)
                        .frame(width: 180, height: 240)
                        .foregroundColor(Color(.white))
   }
}

I’d want to do something like this

Struct ContentView: View {

   let basicPanel = RoundedRectangle(cornerRadius: 24)
                        .frame(width: 180, height: 240)
                        .foregroundColor(Color(.white))

   var body: some View {
      basicPanel
      basicPanel
      basicPanel
   }
}

Sincerely sorry if the question is written terribly, It’s my first question on StackOverflow ever.

>Solution :

Yes, you can just add type declaration some View for variable, like

let basicPanel: some View = RoundedRectangle(cornerRadius: 24)
                    .frame(width: 180, height: 240)
                    .foregroundColor(Color(.white))

Leave a Reply