swiftui - VideoPlayer View 可防止 SwiftUI 导航栏隐藏

标签 swiftui navigationbar navigationview swiftui-navigationlink avkit

当我放 VideoPlayer 时出现问题查看内部 NavigationView的父 View 或 subview 。在这个例子中, subview 将显示导航栏:

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

最佳答案

我遇到了同样的问题。不确定是什么原因造成的,但我最终更换了 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>")

关于swiftui - VideoPlayer View 可防止 SwiftUI 导航栏隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66897332/

相关文章:

ios - 从 Form/NavigationView 中的闭包引用属性并交换 View 时,SwiftUI 内存泄漏

ios - 我不能在 Storyboard 中同时将标题和图像设置到导航栏上的右侧栏项吗?

ios - UINavigationController 在键盘出现崩溃时隐藏栏应用程序 (SWIFT)

swift - NavigationBar 和 TableView 之间的额外空间

SwiftUI导航查看屏幕底部的额外空间

swiftui - NavigationLink 中的选择不起作用

ios - SwiftUI | onDrag - 自定义 (dragItem) 预览图像外观

ios - 如何显示带有预览的上下文菜单?

SwiftUI 导航到 NavigationView 堆栈的底部

android - 即使添加存储库后也无法解析NavigationView