SwiftUI 弹出框在 NavigationView 中时消失 [英] SwiftUI Popover Disappears When It's Inside a NavigationView

查看:32
本文介绍了SwiftUI 弹出框在 NavigationView 中时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 NavigationView 中有一个弹出框:

I have a popover inside of a NavigationView:

import SwiftUI

struct ContentView: View {
  var body: some View {
    NavigationView {
      NavigationLink(destination: ChildView()) {
        Text("Navigate")
      }
    }
    .navigationViewStyle(StackNavigationViewStyle())
  }
}

struct ChildView: View {
  @State private var popover = false

  var body: some View {
    HStack {
      Button(action: { self.popover = true }) {
        Text("Toggle")
      }
      .popover(isPresented: $popover) {
        Text("Yolo")
      }
    }
  }
}

当您在启动应用程序后第一次切换弹出窗口时,它会立即消失.之后它可以正常工作.这是 NavigationView 中的错误吗?有什么解决方法吗?

When you toggle the popover for the first time after starting the app, it immediately disappears. After that it works correctly. Is this a bug in NavigationView? Are there any workarounds?

推荐答案

改用工作表:

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: ChildView()
            ) {
                Text("Navigate")
            }
        }
    }
}

struct ChildView: View {
    @State
    private var isPresented = false
    var body: some View {
        HStack {
            Button(
                action: {
                    isPresented.toggle()
                }) {
                Text("Present")
            }
        }
        .sheet(
            isPresented: $isPresented
        ) {
            Text("Yolo")
        }
    }
}

这篇关于SwiftUI 弹出框在 NavigationView 中时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆