Navigation SwiftUI:使用.onTapGuesture{}导航到另一个视图

Navigation SwiftUI:使用.onTapGuesture{}导航到另一个视图,navigation,swiftui,Navigation,Swiftui,对于下面的CardView,我希望用户点击它来导航到带有post.article\u url中链接字符串的Web视图 我试图用NavigationLink包装CardView。导航成功,但它将CardView上的图像和文本变成了完全蓝色。也许可以通过其他方法解决这个问题,比如使用.onTapGuesture{}中的一些代码 另外,因为这是一个应该被刷卡的卡片堆栈。NavigationLink还会阻止这些卡上的拖动手势 提前谢谢 引入一个变量Bool,使用空的NavigationLink监视该变量

对于下面的CardView,我希望用户点击它来导航到带有post.article\u url中链接字符串的Web视图

我试图用NavigationLink包装CardView。导航成功,但它将CardView上的图像和文本变成了完全蓝色。也许可以通过其他方法解决这个问题,比如使用.onTapGuesture{}中的一些代码

另外,因为这是一个应该被刷卡的卡片堆栈。NavigationLink还会阻止这些卡上的拖动手势


提前谢谢

引入一个变量Bool,使用空的NavigationLink监视该变量。捕获选项卡时,切换此变量,然后激活重定向

// This variable 
@State private var goToNewView: Bool = false

ForEach(self.posts.reversed(), id: \.id) {post in
    GeometryReader{geo -> AnyView in
        return AnyView(
            CardView (index: self.getPostIndex(post: post),
                      title: post.title,
                      image: post.cover_image,
                      author: post.author, 
                      source: post.source, description: post.description,
                      imageWidth: self.cardWidth, 
                      imageHeight: self.imageHeight(index: self.getPostIndex(post: post))
            )
            .animation(.spring())
            .frame(width: self.widthAmount(index: self.getPostIndex(post: post)), 
                   height: self.heightAmount(index: self.getPostIndex(post: post)))
            .offset(x: self.CardOffsetAmountHorizontal(index: self.getPostIndex(post: post)),
                    y: self.CardOffsetAmountVertical(index: self.getPostIndex(post: post)))
            .onTapGesture{
                // navigate to a webview with url: post.article_url

                // Toggle the variable here
                self.goToNewView.toggle()
            }
        )}}


// Put this in your view. It will not be visible
NavigationLink(destination: MyOtherView(), isActive: self.$goToNewView) { EmptyView() }


这将解决NavigationLink中的蓝色问题。这将解释如何以编程方式使用NavigationLink@Andrew Thank,但这仅适用于图像视图。此CardView包含从url获取图像的自定义UrlImageView。如果我直接在CardView上使用.renderingMode.original,则会出现错误并使应用程序崩溃。然后,您的自定义URLMageView应在基础图像的适当位置使用renderingMode。谢谢,它可以工作。我还添加了另一个state变量来携带文章url。但是,当我关闭新页面时,它会以某种方式重新打开自己。只有我第二次关上它,它才不会再出现。有没有办法以编程方式修改新页面?例如,当我关闭此选项时,我可以使goToNewView=false?此外,带有“后退”按钮的顶部栏已占用多行空间和至少三行空间。我还可以修改吗?返回时不应重新打开MyOtherView,除非再次按。返回时,goToNewView的状态将设置回false。在我们的代码中似乎还有其他地方存在问题。
// This variable 
@State private var goToNewView: Bool = false

ForEach(self.posts.reversed(), id: \.id) {post in
    GeometryReader{geo -> AnyView in
        return AnyView(
            CardView (index: self.getPostIndex(post: post),
                      title: post.title,
                      image: post.cover_image,
                      author: post.author, 
                      source: post.source, description: post.description,
                      imageWidth: self.cardWidth, 
                      imageHeight: self.imageHeight(index: self.getPostIndex(post: post))
            )
            .animation(.spring())
            .frame(width: self.widthAmount(index: self.getPostIndex(post: post)), 
                   height: self.heightAmount(index: self.getPostIndex(post: post)))
            .offset(x: self.CardOffsetAmountHorizontal(index: self.getPostIndex(post: post)),
                    y: self.CardOffsetAmountVertical(index: self.getPostIndex(post: post)))
            .onTapGesture{
                // navigate to a webview with url: post.article_url

                // Toggle the variable here
                self.goToNewView.toggle()
            }
        )}}


// Put this in your view. It will not be visible
NavigationLink(destination: MyOtherView(), isActive: self.$goToNewView) { EmptyView() }