iosswiftswiftui

Unable to scale up a video from edge to edge


I am unable to resize a video in the UI to fill up horizontally edge to edge. There is some white space I want to be filled up with the video. I tried a lot of different things I found online but I can only resize it in height.

import SwiftUI
import AVKit

struct VideoMediaView: View {
    let videoURL: URL

    var body: some View {
        VideoPlayer(player: AVPlayer(url: videoURL))
            .aspectRatio(9 / 16, contentMode: .fill) // Ensures the video aspect ratio
            .frame(maxWidth: .infinity) // Makes the video fill the width of the screen
            .frame(height: 560) // Adjust the height if necessary
            .clipped() // Ensures the video content is clipped to its frame
    }
}

Not sure if anything in content view would cause the issue

struct ContentView: View { 
    var body: some View {
        TabView(selection: $selectedTab) {
            
            // Main media view
            NavigationView {
                ScrollView {
                    VStack {
                        // Display media when loaded
                        ForEach(viewModel.mediaData) { media in
                            MediaCardView(media: media)
                                .padding(.vertical, 8)
                                .padding(.horizontal, 16)
                        }
                    }
                }
                .onAppear {
                    viewModel.loadStoredData() // Load stored media when app starts
                }
            }
            .tabItem {
                Image(systemName: "photo.on.rectangle")
                Text("Media")
            }
            .tag(0)
            
            // Stats view for metrics
            StatsView() // The new view for metrics
                .tabItem {
                    Image(systemName: "chart.bar.fill")
                    Text("Stats")
                }
                .tag(1)
        }
    }
}

enter image description here


Solution

  • "fill the width of the screen" isn't quite correct. It fills the rest of the "allocated" space.

    The parent is taking up space, just remove the horizontal padding from the ContentView