swift - SwiftUI Function 声明一个 Opaque 返回类型,在一个 View 中插入两个按钮?
问题描述
我想构建一个简单的 SwiftUI ContentView.swift。在此应用程序的视图窗格中,它将包含两个按钮,其中隐含了它们的图像。我想过为每个按钮一个一个添加一个功能,然后让 SwiftUI 显示每个元素。我已经查看了一些与返回类型相关的问题,尽管如果我只有一个 Button(),我会在哪里添加返回类型让我感到困惑。我的代码很短,在我没有包含返回的地方很容易看出我哪里出错了?
struct ContentView: View {
var body : some View {
func Button1(){
Button(action: {
print("Here is a bit of information.")
}) {
Image(systemName: "info")
.padding()
.background(Color.green)
.font(.largeTitle)
.foregroundColor(Color.orange)
.frame(width: 300, height: 600)
}
}
func Button2(){
Button(action: {
print("You have erased it.")
}) {
Image(systemName: "trash")
.padding()
.background(Color.red)
.font(.largeTitle)
.foregroundColor(Color.white)
.frame(width: 426, height: 620)
}
}
}
我希望这两个按钮会出现在屏幕的第一个视图上,然后我可以在我了解代码中的位置后编辑它们将采取的动作。感谢您的见解 :)
解决方案
以下是您将如何执行此操作:
struct ContentView: View {
var body: some View {
VStack {
Button(action: {
print("Here is a bit of information.")
}) {
Image(systemName: "info")
.padding()
.background(Color.green)
.font(.largeTitle)
.foregroundColor(Color.orange)
.frame(width: 300, height: 600)
}
Button(action: {
print("You have erased it.")
}) {
Image(systemName: "trash")
.padding()
.background(Color.red)
.font(.largeTitle)
.foregroundColor(Color.white)
.frame(width: 426, height: 620)
}
}
}
}
推荐阅读
- javascript - Javascript - 'import' 和 'export' 语句在哪里合法?
- fortran - 使用未在数组赋值中分配的可分配数组
- javascript - 检查对象数组以查看特定属性是否与条件匹配
- python - numpy 取许多样本而不按行替换
- dart - dart:通用:未使用局部变量“”的值
- angular - 设备 ####### 在操作安装过程中丢失消息 ######
- c# - 蛇程序无异常停止运行
- c - 如果字符串重复只返回一次重复的字母,有没有办法?
- javascript - 不确定在哪里插入我的进度条组件以进行 api 调用
- terraform - http 提供者/数据源 - 懒惰还是急切?