静态方法'buildBlock'要求'Button <Any> .Type'符合'View'

时间:2019-09-28 05:31:18

标签: swiftui

swiftui的新手。我该如何解决?


struct ContentView: View {
    var body: some View {
        HStack {
            VStack {
                Text("Hello World")
                .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
            VStack {
                Button(action:  {
                    self.volumeCheck()
                }) {
                    Image("chimes")
                      .renderingMode(Image.TemplateRenderingMode?.init(Image.TemplateRenderingMode.original))

                }
                Button(action: {
                    self.volumeCheck()
                }) {
                    Text("Click chimes to test volume")
                }
            }
        }

    }

    func volumeCheck() {

    }
}


报告

static method 'buildBlock' requires that 'Button<Any>.Type' conform to 'View'

----------------------------------------

failedToBuildDylib: /Users/scottlydon/Library/Autosave Information/SmartWorkTimer/SmartWorkTimer/ContentView.swift:18:20: error: static method 'buildBlock' requires that 'Button<Any>.Type' conform to 'View'
            VStack {
                   ^
SwiftUI.ViewBuilder:3:24: note: where 'C1' = 'Button<Any>.Type'
    public static func buildBlock<C0, C1>(_ c0: C0, _ c1: C1) -> TupleView<(C0, C1)> where C0 : View, C1 : View
                       ^
/Users/scottlydon/Library/Autosave Information/SmartWorkTimer/SmartWorkTimer/ContentView.swift:26:17: error: generic parameter 'Label' could not be inferred
                Button
                ^
/Users/scottlydon/Library/Autosave Information/SmartWorkTimer/SmartWorkTimer/ContentView.swift:26:17: note: explicitly specify the generic arguments to fix this issue
                Button
                ^
                      <<#Label: View#>>

1 个答案:

答案 0 :(得分:-1)

您必须将UIKIT集成到项目中,并使UIButton符合UIViewRepresentable和UIViewControllerRepresentable,才能将现有的UIKit组件移植到SwiftUI。最简单的示例之一是UIButton,在编写本文时还没有相应的SwiftUI组件。

struct ButtonView: UIViewRepresentable {


func makeUIView(context: UIViewRepresentableContext<ButtonView>) -> UIButton {
    return UIButton(frame: sampleFRame)
}

func updateUIView(_ uiView: UIButton, context: UIViewRepresentableContext<ButtonView>) {
    
}

}

//内部Swift用户界面

 var body: some View {
    VStack {
        ButtonView().onTapGesture {
                print("Tapped")
        }
        Spacer()
    }
}