我有2个快速文件:
ContentView.swift
struct ContentView: View {
var body: some View {
NavigationView {
ZStack(alignment: .leading) {
Color.white.edgesIgnoringSafeArea(.all)
SetBackground
}
}
}
}
和我创建的swiftui视图文件称为:
Background.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
这是为什么,我该如何解决?
谢谢
答案 0 :(得分:1)
您必须通过初始化对象来使用View
,而不是使用SetBackground
类型。
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 "()"
}
}
}
}