首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类型'Background.Type‘不能符合’视图‘;只有结构/枚举/类类型SwiftUI

类型'Background.Type‘不能符合’视图‘;只有结构/枚举/类类型SwiftUI
EN

Stack Overflow用户
提问于 2020-04-15 03:13:55
回答 1查看 1.8K关注 0票数 3

我有两个快速档案:

ContentView.swift

代码语言:javascript
运行
复制
struct ContentView: View {
    var body: some View {
        NavigationView {
            ZStack(alignment: .leading) {

                Color.white.edgesIgnoringSafeArea(.all)

                SetBackground
            }
        }
    }
}

我创建的迅捷视图文件名为:

Background.swift

代码语言:javascript
运行
复制
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

这是为什么,我怎么能修好它?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-15 03:22:12

您必须通过初始化对象来使用View,而不是使用SetBackground类型。

代码语言:javascript
运行
复制
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 "()"
            }
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61220727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档