skip to Main Content

I am using a .fullScreenCover to present a VideoPlayer view. On iOS 15 and below, doing so shows the close button in the top-left, like below:

enter image description here

But on iOS 16, it doesn’t seem to appear.

enter image description here

Code is below:

.fullScreenCover(
        isPresented: $showVideoSheet,
        onDismiss: {
            selectedVideo = nil
        },
        content: {
            if let video = selectedVideo {
                VideoPlayerView(video: video)
            }
        }
    )

VideoPlayerView is below:

struct VideoPlayerView: View {

    @StateObject var model: VideoPlayerViewModel
    let video: VideoModel

    init(video: VideoModel) {
        self._model = StateObject(wrappedValue: VideoPlayerViewModel(video: video))
        self.video = video
    }

    var body: some View {
        if let player = model.player {
            VideoPlayer(player: player)
                .ignoresSafeArea()
        }
    }
}

The view model just loads the AVPlayer etc.

Any suggestions?

3

Answers


  1. You can make your own close button.

    @State var shouldShowVideoPlayer = false
    
    var body: some View {
        Text("Example")
        .fullScreenCover(
            isPresented: $shouldShowVideoPlayer,
            onDismiss: {
                selectedVideo = nil
            },
            content: {
                ZStack(alignment: .topLeading) {
                    if let video = selectedVideo {
                        VideoPlayerView(video: video)
                    }
    
                    Button {
                        shouldShowVideoPlayer = false
                    } label: {
                        Image(systemName: "xmark")
                    }
                    .background(Color.white)
                    .padding(20)
                }
            }
        )
    }
    
    Login or Signup to reply.
  2. I’m facing the exact same situation and I solved it similarly to what Tamás suggested. I added a close button only for versions starting at iOS 16, given that former versions already present the button.
    Also, I tried to design the button similarly to the sound button already existing on the top rightmost side of the screen which now doesn’t contain a surface around it.

    VideoPlayer(player: AVPlayer(playerItem: AVPlayerItem(asset: asset))) {
        if #available(iOS 16.0, *) {
            VStack {
                HStack {
                    Button {
                        canOpenVideo = false
                    } label: {
                        Image(systemName: "xmark")
                            .resizable()
                            .renderingMode(.template)
                            .frame(width: 15, height: 15)
                            .foregroundColor(.white)
                    }
                    .padding()
                    
                    Spacer()
                }
                
                Spacer()
            }
        }
    }
    
    Login or Signup to reply.
  3. You can create player using UIViewControllerRepresentable instead VideoPlayer.

    struct YourView: View {
        var body: some View {
           VideoPlayerView(player: AVPlayer(url: URL(string: "your_url")!))
        }
    }
    
    struct VideoPlayerView: UIViewControllerRepresentable {
        let player: AVPlayer
    
        func makeUIViewController(context: Context) -> AVPlayerViewController {
            let controller = AVPlayerViewController()
            controller.player = player
    
            return controller
        }
    
        func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
    
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search