Swiftui 静态法';构建块';要求';按钮<;任何>;。类型';符合';视图';
swiftui的新功能。我如何解决这个问题Swiftui 静态法';构建块';要求';按钮<;任何>;。类型';符合';视图';,swiftui,Swiftui,swiftui的新功能。我如何解决这个问题 struct ContentView: View { var body: some View { HStack { VStack { Text("Hello World") .frame(maxWidth: .infinity, maxHeight: .infinity) } VStack {
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#>>
静态方法“buildBlock”要求“Button.Type”符合“View”
----------------------------------------
failedToBuildDylib:/Users/scottlydon/Library/Autosave Information/SmartWorkTimer/SmartWorkTimer/ContentView。swift:18:20:错误:静态方法“buildBlock”要求“按钮。类型”与“视图”一致
VStack{
^
SwiftUI.ViewBuilder:3:24:注意:其中“C1”=“按钮.类型”
公共静态func构建块(c0:c0,c1:c1)->TupleView,其中c0:View,c1:View
^
/Users/scottlydon/Library/Autosave Information/SmartWorkTimer/SmartWorkTimer/ContentView.swift:26:17:错误:无法推断通用参数“Label”
按钮
^
/Users/scottlydon/Library/Autosave Information/SmartWorkTimer/SmartWorkTimer/ContentView.swift:26:17:注意:显式指定用于修复此问题的通用参数
按钮
^
您必须在项目中集成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>) {
}
}
我总是将按钮构建为
按钮(操作:{},标签:{})
,我认为您也应该这样尝试。错误消息似乎表明标签
丢失。当我将上述代码粘贴到Xcode中时,没有错误,您使用的是哪个版本?此处相同Xcode 11.2.1->无错误
var body: some View {
VStack {
ButtonView().onTapGesture {
print("Tapped")
}
Spacer()
}
}