swiftui - VideoPlayer 视图防止 SwiftUI 导航栏隐藏
问题描述
当我将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())
}
}
}
解决方案
我遇到了同样的问题。不知道是什么原因造成的,但我最终用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>")
推荐阅读
- git - git diff 没有差异,但由于行尾改变不应该有一些差异?
- kernel - 现代计算机系统(x86 架构)是否具有独立于操作系统的虚拟寻址?
- python - 什么是 tensorflow.python.data.ops.dataset_ops._OptionsDataset?
- c# - 类的 foreach 函数
- spring-boot - 我正在尝试使用 Spring webflux 使用弹性搜索 API 以使我的 API 端点非阻塞
- c# - 有没有办法从另一个进程“GetCommandLineArgs”?
- python - 在生成器表达式或 for 循环中使用 python lambda
- python - 从 Python 中打开的类似文件的对象解析 Mbox?
- r - 循环遍历 R 中的数据框以更改列名和行名
- android - 使用改造客户端不允许操作错误