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

标签 swiftui

我正在 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/

相关文章:

SwiftUI 安全字段 : How to achieve the same character obscuring behaviour as in UIKit?

ios - SwiftUI Split View大标题不适用于 iPad

swift - 用户凭证 - 使用 Apple 登录 (SwiftUI)

swiftui - .sheet : Shows only once and then never again

ios - 应用程序崩溃,并调用了 [ServicesDaemonManager] 中断处理程序。 -[FontServicesDaemonManager 连接]_block_invoke

ios - 尽管属性已更改,但应用程序不会立即更新 View

swift - 如何在 SwiftUI tvOS 中检测 'Click' 手势

ios - 如何修复自定义按钮在 SwiftUI(IOS) 中的交互?

ios - SwiftUI 2 个问题,但很简单,关于 searchaBle 和 List 的背景图片

iOS 按钮和导航链接在列表中彼此相邻