首页 > 解决方案 > VStack 中的按钮和文本

问题描述

我正在尝试在 SwiftUI 中定义以下视图,但它不起作用:

struct ContentView: View {
    var body: some View {
        Text("Placeholder")
        Button(action: {
            // Do something
        }) {
            Text("Button")
        }
    }
}

错误是:

Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type

还有两个警告:

Result of 'Text' initializer is unused

Result of 'Button<Label>' initializer is unused

我正在尝试在带有 OS Catalina 的 Mac 上使用 XCode11 进行编码。有谁知道问题是什么?

标签: swiftui

解决方案


你忘了添加提到的VStack

var body: some View {
    VStack {                // << here !!
       Text("Placeholder")
       Button(action: {
        // Do something
       }) {
          Text("Button")
       }
    }
}

推荐阅读