我有两个swift文件:
ContentView.swift
struct ContentView: View {
var body: some View {
NavigationView {
ZStack(alignment: .leading) {
Color.white.edgesIgnoringSafeArea(.all)
SetBackground
}
}
}
}
我创建的swiftui视图文件名为:
背景.swift
struct SetBackground: View {
var body: some View {
GeometryReader { geometry in
Capsule()
.foregroundColor(.yellow)
.frame(width: geometry.size.width * 1.7)
.offset(x: geometry.size.width * -0.1 , y: geometry.size.height * -0.9)
}
}
}
当我试图在第一段代码中调用文件Background时,我得到了错误:Type 'SetBackground.Type' cannot conform to 'View'; only struct/enum/class types can conform to protocols
为什么会出现这种情况,我该如何解决?
感谢
您必须通过初始化对象而不是使用SetBackground
类型来使用View
。
struct ContentView: View {
@State var dataStore = [0, 1, 2]
@State var a = ""
var body: some View {
NavigationView {
ZStack(alignment: .leading) {
Color.white.edgesIgnoringSafeArea(.all)
SetBackground() <---- add "()"
}
}
}
}