视频播放器视图阻止SwiftUI导航栏隐藏。

7
问题在于我将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())
    }
  }
}
2个回答

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>) { }
}

根据您的使用场景,执行以下操作:

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

谢谢!这个解决方案可行。但是我遇到了另一个问题,CustomPlayer不能使用动态源。我尝试传递AVPlayer而不是src,但仍在寻找解决方案以使其在更改时更新源。 - netsplatter

2

如果您想要一个具有与原生播放器相同的初始化程序并且随着应用程序的当前状态更新的自定义播放器:

struct CustomPlayer: UIViewControllerRepresentable {
    let player: AVPlayer

    func makeUIViewController(context: UIViewControllerRepresentableContext<CustomPlayer>) -> AVPlayerViewController {
        let controller = AVPlayerViewController()
        controller.player = player
        return controller
    }

    func updateUIViewController(_ uiViewController: AVPlayerViewController, context: UIViewControllerRepresentableContext<CustomPlayer>) {
        uiViewController.player = player
    }
}


请注意,在updateUIViewController(_: context:)方法中,您应该更新每个声明的变量并期望进行更新的视图。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接