当我放 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/