Home > Back-end >  Is it possible to define a RoundedRectangle with some properties as a variable for later usage in Sw
Is it possible to define a RoundedRectangle with some properties as a variable for later usage in Sw

Time:05-04

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.

CodePudding user response:

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))
  • Related