1

When I put AVPlayerViewController wrapped into UIViewControllerRepresentable as a fullscreen background of my view, it forces the main content view to ignore safe area.

 var body: some View {
        VStack(spacing: 0) {
            HStack {
                Text("Some text")
                Spacer()
            }
            Spacer()
        }
        .padding(16)
        .foregroundColor(Color.white)
        .background(
            VideoPlayerController(videoURL: videoURL, isPlaying: $isPlaying)
                .edgesIgnoringSafeArea(.all)
        )
    }

In the example above the text ignores safe area.

VideoPlayerController:

struct VideoPlayerController: UIViewControllerRepresentable {
    typealias UIViewControllerType = AVPlayerViewController
    
    var videoURL: URL
    @Binding var isPlaying: Bool
    let showPlaybackControls = false
    
    func makeUIViewController(context: Context) -> AVPlayerViewController {
        let player = AVPlayer(url: videoURL)
        let playerController = AVPlayerViewController()
        
        playerController.player = player
        playerController.showsPlaybackControls = showPlaybackControls
        
        return playerController
    }
    
    func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
        if isPlaying {
            uiViewController.player?.play()
        } else {
            uiViewController.player?.pause()
        }
    }
}

But if I make the background a plain view, the text doesn't ignore safe area anymore

struct ExerciseVideoScreen: View {
    let videoURL: URL
    @State var isPlaying: Bool = false
    
    var body: some View {
        VStack(spacing: 0) {
            HStack {
                Text("Some text")
                Spacer()
            }
            Spacer()
        }
        .padding(16)
        .foregroundColor(Color.white)
        .background(
            Color.red
                .edgesIgnoringSafeArea(.all)
        )
    }
}

Is there a way to resolve this issue?

AlexKost
  • 2,792
  • 4
  • 22
  • 42
  • I think this is due to specific of AVPlayer full screen mode. And for views above it controller has specific `contentOverlayView` property. You should try that way. – Asperi Dec 01 '21 at 08:27
  • @Asperi I don't see how I can apply this in context of SwiftUI. Would you please show me some brief working example? – AlexKost Dec 01 '21 at 09:50

0 Answers0