如何声明正确的内容以将另一个 swiftui 文件导入到 contentview 文件中?
How can I declare the correct content to import another swiftui file into the contentview file?
我对 swift 有点陌生,对问我的问题有点困惑。一些背景:我在网上找到一个人,他为滑动底部视图栏创建了一个 swiftui 文件,我想将它实现到我的 ContentView swiftui 文件中。在 xcode 中单击 "fix" 后,我在互联网上查了查并尝试玩弄一些东西,但我没有运气。
这里是错误的图片,我点击 xcode 的 "fix" 提示,BottomSheetView.swift 文件的代码 -> https://imgur.com/a/GqEjMzo
谁能帮我解释一下 swift 到底要我做什么,并让我知道如何解决这个错误?
这是我的代码:
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack{
VStack{
MapView()
.edgesIgnoringSafeArea(.all)
let heightDouble = CGFloat(150.00)
BottomSheetView(isOpen: .constant(true), maxHeight: heightDouble, content: <#() -> Content#>)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
SwiftUI 中的错误并不总是显示在实际位置。您的代码段中的问题在于:
let heightDouble = CGFloat(150.00)
它应该是:包含声明的闭包不能与函数生成器一起使用'ViewBuilder'
试试这个,你会发现问题不在其他视图中:
var body: some View {
ZStack{
VStack{
Text("hello") // replaced MapView()
.edgesIgnoringSafeArea(.all)
// you need to delete this row to avoid error
let heightDouble = CGFloat(150.00)
Text("world") // replaced BottomSheetView(...)
}
}
}
body
变量中的代码必须returnsome View
,但是在其中定义常量违反了这个规则
更新
在 BottomSheetView
中你有 content
,这是另一个 View
。我没有看到所有的代码,但我认为它应该是这样的:
struct ContentView: View {
@State var isOpen = true
var body: some View {
ZStack{
VStack{
MapView()
.edgesIgnoringSafeArea(.all)
BottomSheetView(isOpen: self.$isOpen, maxHeight: CGFLoat(150)) {
Text("bottom")
}
}
}
}
我对 swift 有点陌生,对问我的问题有点困惑。一些背景:我在网上找到一个人,他为滑动底部视图栏创建了一个 swiftui 文件,我想将它实现到我的 ContentView swiftui 文件中。在 xcode 中单击 "fix" 后,我在互联网上查了查并尝试玩弄一些东西,但我没有运气。
这里是错误的图片,我点击 xcode 的 "fix" 提示,BottomSheetView.swift 文件的代码 -> https://imgur.com/a/GqEjMzo
谁能帮我解释一下 swift 到底要我做什么,并让我知道如何解决这个错误?
这是我的代码:
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack{
VStack{
MapView()
.edgesIgnoringSafeArea(.all)
let heightDouble = CGFloat(150.00)
BottomSheetView(isOpen: .constant(true), maxHeight: heightDouble, content: <#() -> Content#>)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
SwiftUI 中的错误并不总是显示在实际位置。您的代码段中的问题在于:
let heightDouble = CGFloat(150.00)
它应该是:包含声明的闭包不能与函数生成器一起使用'ViewBuilder'
试试这个,你会发现问题不在其他视图中:
var body: some View {
ZStack{
VStack{
Text("hello") // replaced MapView()
.edgesIgnoringSafeArea(.all)
// you need to delete this row to avoid error
let heightDouble = CGFloat(150.00)
Text("world") // replaced BottomSheetView(...)
}
}
}
body
变量中的代码必须returnsome View
,但是在其中定义常量违反了这个规则
更新
在 BottomSheetView
中你有 content
,这是另一个 View
。我没有看到所有的代码,但我认为它应该是这样的:
struct ContentView: View {
@State var isOpen = true
var body: some View {
ZStack{
VStack{
MapView()
.edgesIgnoringSafeArea(.all)
BottomSheetView(isOpen: self.$isOpen, maxHeight: CGFLoat(150)) {
Text("bottom")
}
}
}
}