VideoPlayer 视图可防止 SwiftUI 导航栏隐藏

Mik*_*oon 7 navigationbar avkit navigationview swiftui swiftui-navigationlink

VideoPlayer当我将view 放入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())
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 4

我也有同样的问题。不知道是什么原因造成的,但我最终VideoPlayer用定制的替换了它。这消除了顶部的空间。

  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>) { }
}
Run Code Online (Sandbox Code Playgroud)

在你看来你想在哪里使用它,请执行以下操作:

CustomPlayer(src: "<the source to the video>")
Run Code Online (Sandbox Code Playgroud)