gpt4 book ai didi

swiftui - 如何在 SwiftUI 的预览参数中传递类型 Namespace.ID?

转载 作者:行者123 更新时间:2023-12-04 00:51:39 25 4
gpt4 key购买 nike

我正在 SwiftUI 中创建一个带有几个参数的 subview 。参数的类型之一是 Namspace.ID。所以我必须通过预览传递 Namespace.ID 类型参数才能在 Xcode Canvas 中查看 View 。我不知道该怎么做。我的代码看起来像这样。

    struct BagView: View {
var bagData:BagModel
var animation:Namespace.ID
var body: some View {
VStack{
ZStack{
Color(bagData.image)
.cornerRadius(15)
Image(bagData.image)
.resizable()
.aspectRatio(contentMode: .fit)
.padding(20)
.matchedGeometryEffect(id: bagData.image, in: animation)
}
Text(bagData.title)
.fontWeight(.heavy)
.foregroundColor(.gray)
Text(bagData.price)
.fontWeight(.heavy)
.foregroundColor(.black)
}
}
}

struct BagView_Previews: PreviewProvider {
static var previews: some View {
BagView(bagData: BagModel(image: "bag1", title: "The Bag", price: "$123"))
// I am getting an error here: Missing argument for parameter 'animation' in call
}
}
如何通过解决错误来查看 Canvas 中的 View ?

最佳答案

这是可能的方法 - 使用中间测试/预览 View :

struct BagView_Previews: PreviewProvider {
struct TestView: View {
@Namespace var ns
var body: some View {
BagView(bagData: BagModel(image: "bag1", title: "The Bag", price: "$123"),
animation: ns)
}
}
static var previews: some View {
TestView()
}
}

关于swiftui - 如何在 SwiftUI 的预览参数中传递类型 Namespace.ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65934401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com