Search code examples
iosswiftuiuicontrol

Use UIControl Component in SwiftUI


I'm first in SwiftUI and IOS. I want to use SimpleCheckBox in my SwiftUI.

But I only get

Error:(14, 18) static method 'buildBlock' requires that 'Checkbox' conform to 'View'

This is my code.

var body: some View {
  HStack {
    Checkbox(frame: CGRect(x: 50, y: 50, width: 25, height: 25))
    Text("HelloWorld!")

  }
}

How can I use UIControl in SwiftUI?


Solution

  • Writting a custom CheckBox in SwiftUI is not a lot of work. This is a working minimum version:

    struct CheckBox: View {
    
        @Binding var isSelected: Bool
    
        var body: some View {
            Button(action: { self.isSelected.toggle() }) {
                Image(systemName: self.isSelected ? "checkmark.circle" : "circle")
            }
        }
    }