斯威夫伊新手。我该如何解决这个问题?
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() {
}
}
Run Code Online (Sandbox Code Playgroud)
报告
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#>>
Run Code Online (Sandbox Code Playgroud)
我在 Xcode 13.1 中遇到了同样的错误(静态方法“buildBlock”要求“Button”符合“View”)。并非所有项目都如此,只是部分项目如此。令人惊讶的是,对我有帮助的是按以下方式使用 Button(只需加上 SwiftUI 前缀):
SwiftUI.Button
您必须将 UIKIT 集成到您的项目中,并使 UIButton 符合 UIViewRepresentable 和 UIViewControllerRepresentable 让您可以将现有的 UIKit 组件移植到 SwiftUI。最简单的示例之一是 UIButton,在撰写本文时,它没有\xe2\x80\x99t 相应的 SwiftUI 组件。
\nstruct ButtonView: UIViewRepresentable {\n\n\nfunc makeUIView(context: UIViewRepresentableContext<ButtonView>) -> UIButton {\n return UIButton(frame: sampleFRame)\n}\n\nfunc updateUIView(_ uiView: UIButton, context: UIViewRepresentableContext<ButtonView>) {\n \n}\n\n}\nRun Code Online (Sandbox Code Playgroud)\n// 快速用户界面内部
\n var body: some View {\n VStack {\n ButtonView().onTapGesture {\n print("Tapped")\n }\n Spacer()\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
13867 次 |
| 最近记录: |