我有两个快速档案:
ContentView.swift
struct ContentView: View {
var body: some View {
NavigationView {
ZStack(alignment: .leading) {
Color.white.edgesIgnoringSafeArea(.all)
SetBackground
}
}
}
}我创建的迅捷视图文件名为:
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)
}
}
}当我试图在第1段代码中调用文件背景时,我得到了错误:Type 'SetBackground.Type' cannot conform to 'View'; only struct/enum/class types can conform to protocols
这是为什么,我怎么能修好它?
谢谢
发布于 2020-04-15 03:22:12
您必须通过初始化对象来使用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 "()"
}
}
}
}https://stackoverflow.com/questions/61220727
复制相似问题