3

My Code looks something like this:

import SwiftUI

struct MainView: View {
    var body: some View {
        CGRect(x: 20, y: 20, width: 100, height: 100)
    }
}

However, I get the error:

Static method 'buildBlock' requires that 'CGRect' conform to 'View'

How can I use CGRect with SwiftUI?

Johnas
  • 296
  • 2
  • 5
  • 15
  • 2
    I would work through [Apple's SwiftUI tutorial] (https://developer.apple.com/tutorials/swiftui/). You are entering a wholly new paradigm from UIKit. – Yrb Nov 07 '20 at 19:38
  • @matt I tried to draw a rectangle, I didn't understood that well before that CGRect are just some measurements. – Johnas Nov 08 '20 at 08:59

1 Answers1

8

Probably you wanted this

struct MainView: View {
    var body: some View {
        Rectangle()
          .frame(width: 100, height: 100)
    }
}

in SwiftUI we should just place views in body, instead of drawing something.

Note: there are different variants for view layout on screen, depending on needs, but I'd recommend to avoid position hardcoding (like x:20, y:20), because it will give different result on different devices.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690