Swift初学者:如何用AVFoundation让视频填满Storyboard设置的帧
Fixing Video Framing to Fill Your Storyboard Space in Swift
Hey there! I see you're getting started with Swift and AVFoundation, and you're stuck on getting your video to fill the space you set up in Storyboard. Let's walk through exactly what you need to do:
Step 1: Set Up Your Video Container in Storyboard
First, you need a dedicated view to hold your video:
- Drag a
UIViewonto your View Controller's main view in Storyboard. - Add constraints to make this view fill the space you want (e.g., pin top, bottom, leading, trailing to the parent view, or set specific width/height constraints).
- Connect this view to your View Controller as an IBOutlet—let's name it
videoContainerView.
Step 2: Add AVPlayer & AVPlayerLayer Logic to Your Code
The key here is using AVPlayerLayer (which handles rendering the video) and setting its videoGravity property to control how the video fills the container. Here's the updated code:
import UIKit import AVFoundation import AVKit class ViewController: UIViewController { // IBOutlet for your video container view from Storyboard @IBOutlet weak var videoContainerView: UIView! // Keep strong references to these so they don't get deallocated private var player: AVPlayer? private var playerLayer: AVPlayerLayer? override func viewDidLoad() { super.viewDidLoad() setupVideoPlayer() } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() // Update the player layer's frame when the view layout changes (e.g., screen rotation) playerLayer?.frame = videoContainerView.bounds } private func setupVideoPlayer() { // Replace with your video URL (local or remote) guard let videoURL = Bundle.main.url(forResource: "your-video-file", withExtension: "mp4") else { print("Couldn't find video file") return } // Initialize the player player = AVPlayer(url: videoURL) // Create the player layer and configure it playerLayer = AVPlayerLayer(player: player) guard let playerLayer = playerLayer else { return } // This is the critical setting to fill the container // .resizeAspectFill: Fills the container, cuts off parts of the video if needed // .resizeAspect: Keeps video aspect ratio, adds black bars if needed playerLayer.videoGravity = .resizeAspectFill // Set the layer's frame to match the container view playerLayer.frame = videoContainerView.bounds // Add the layer to the container view's layer hierarchy videoContainerView.layer.addSublayer(playerLayer) // Start playing the video (optional) player?.play() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() player?.pause() player = nil playerLayer = nil } }
Key Explanations:
videoGravity: This property controls how the video scales to fit its layer. Use.resizeAspectFillif you want the video to completely fill the container (it will maintain aspect ratio but may crop edges). If you prefer no cropping (even if it leaves black bars), use.resizeAspect.viewDidLayoutSubviews: We update the player layer's frame here because the container view's bounds might change (like when the device rotates), ensuring the video always fills the space.- Strong References: We keep
playerandplayerLayeras properties so they aren't automatically released by ARC, which would stop the video from playing.
That should get your video filling the exact space you set up in Storyboard!
内容的提问来源于stack exchange,提问作者Kylie Ritchie




