VideoPlayer视图防止SwiftUI导航栏隐藏

VideoPlayer视图防止SwiftUI导航栏隐藏,swiftui,navigationbar,navigationview,swiftui-navigationlink,avkit,Swiftui,Navigationbar,Navigationview,Swiftui Navigationlink,Avkit,当我将VideoPlayer视图放在NavigationView的父视图或子视图中时,就会出现问题。在此示例中,子视图将显示导航栏: struct ParentView: View { var body: some View { NavigationView { VStack { Text("Parent View") NavigationLink(destination: ChildView().navigation

当我将
VideoPlayer
视图放在
NavigationView
的父视图或子视图中时,就会出现问题。在此示例中,子视图将显示导航栏:

struct ParentView: View {
  var body: some View {
    NavigationView {
      VStack {
        Text("Parent View")

        NavigationLink(destination: ChildView().navigationBarHidden(true)) {
          Text("Child View")
        }
      }
      .navigationBarHidden(true)
    }
  }
}

struct ChildView: View {
  var body: some View {
    VStack {
      Text("Child View")
      VideoPlayer(player: AVPlayer())
    }
  }
}

我也有同样的问题。不确定是什么原因导致的,但我最终用一个定制的
视频播放器替换了
。这样就去掉了顶部的空间

  struct CustomPlayer: UIViewControllerRepresentable {
  let src: String

  func makeUIViewController(context: UIViewControllerRepresentableContext<CustomPlayer>) -> AVPlayerViewController {
    let controller = AVPlayerViewController()
    let player = AVPlayer(url: URL(string: src)!)
    controller.player = player
    player.play()
    return controller
  }

  func updateUIViewController(_ uiViewController: AVPlayerViewController, context: UIViewControllerRepresentableContext<CustomPlayer>) { }
}
struct CustomPlayer:UIViewControllerRepresentable{
让src:String
func makeUIViewController(上下文:UIViewControllerRepresentableContext)->AVPlayerViewController{
let controller=avplayervewcontroller()
let player=AVPlayer(url:url(string:src)!)
controller.player=player
player.play()
返回控制器
}
func updateUIViewController(uViewController:AVPlayServiceWCONTROLLER,上下文:UIViewControllerRepresentableContext){}
}
在您想使用它的地方,请执行以下操作:

CustomPlayer(src: "<the source to the video>")
CustomPlayer(src:)

谢谢!这个解决方案有效。但是CustomPlayer还有一个问题——它不适用于动态源代码。我试图通过AVPlayer而不是src,但仍然在寻找解决方案,使其在更改时更新源代码。