Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading