swift - 在这种情况下,如何使NavigationLink工作?我可以在其中添加按钮吗?

标签 swift xcode swiftui

HStack {
                VStack {
                    NavigationLink(destination: Convos()) {
                        Text("Go to Convos")
                    }
                    ZStack {
                        Circle()
                            .fill(Color.white)
                            .frame(width: 55, height: 55, alignment: .center)
                            .shadow(color: .gray, radius: 1.5, y: 3)
                        Image(systemName: "map").resizable().frame(width: 30, height: 30).foregroundColor(Color("purple"))
                    }
                }
}


我需要在该圆圈上方有一个带地图的按钮,该按钮会将我带到另一个视图,即Convos()。我已经尝试添加NavigationView和Button,但是不确定是否编写了正确的语法。有其他方法可以做到这一点吗?而不是像从普通的List / NavigationLink那样从右侧进入视图,有没有办法使View从左侧或底部进入/选择?谢谢

最佳答案

您唯一要做的就是将代码包装在NavigationView中:

struct ExampleView: View {
    var body: some View {
        NavigationView {
            // your code snippet from above
        }
    }
}


至于自定义过渡,我认为现在NavigationView仅支持默认的“从右边滑动”过渡。

如果您使用的是全页面浏览量,则可以尝试使用类似的方法

struct ExampleView: View {
    @State private var showingConvos = false

    var body: some View {
        ZStack {
            HStack {
                VStack {
                    Button(action: {
                        self.showingConvos.toggle()
                    }) {
                        Text("Go to Convos")
                    }
                    ZStack {
                        Circle()
                            .fill(Color.white)
                            .frame(width: 55, height: 55, alignment: .center)
                            .shadow(color: .gray, radius: 1.5, y: 3)
                        Image(systemName: "map").resizable().frame(width: 30, height: 30).foregroundColor(Color("purple"))
                    }
                }
            }

            // Convos view will appear when showingConvos == true
            if showingConvos {
                Convos(showingConvos: $showingConvos)
                    // customize the transition and animation to suit your preference
                    .transition(.move(edge: .bottom))
                    .animation(.easeIn)
            }
        }
    }
}


而且您的Convos视图将需要绑定,以便您可以将其关闭:

    struct Convos: View {
    @Binding var showingConvos: Bool

    var body: some View {
        ZStack {
            Rectangle()
                .fill(Color.gray)
                .edgesIgnoringSafeArea(.all)

            // Example back button
            Button(action: {
                self.showingConvos.toggle()
            }) {
                Text("Back")
            }
        }
    }
}

关于swift - 在这种情况下,如何使NavigationLink工作?我可以在其中添加按钮吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58701073/

相关文章:

swift - iOS 状态栏和导航栏颜色相同

ios - 使用私有(private) API 读取 WiFi RSSI 值

ios - UICollectionViewCells 在 reloadData() 后不显示

swift - NSDateFormatter 做错了

ios - 关于 xcode Assets 目录和应用程序大小

iphone - 更改产品名称会导致问题吗?

ios - 在 XCode 中为设备/模拟器编译不同的文件

text - 如何在 SwiftUI 文本中使用自定义字体使我的文本加粗?

ios - 如何在 SwiftUI 中引用外部 iOS 系统状态更新?

swift - 如何将接收器结果分配给变量