2

I would like to be able to force an AVPlayerView to have a 16:9 aspect ratio, even if the user decides to resize the window.

Right now I have it so that my view opens automatically with a 16:9 ratio (800x450)

Screenshot: https://i.stack.imgur.com/xOKg1.jpg

the problem is, if the user decides to resize the window, the aspect ratio can get skewed and would result in large black rectangles around the video.

Screenshot: https://i.stack.imgur.com/T6Iqt.jpg

Any possible ways of effectively keeping/forcing the aspect ratio in swift?

user3697446
  • 113
  • 1
  • 9
  • Are you using a storyboard for the constraints? – Sethmr Jun 26 '16 at 16:57
  • no, basically just a viewcontroller, with a AVKit Player View as its root. I just figured it out though. Just needed to add . self.view.window?.aspectRatio = NSSize(width: 16, height: 9) into viewDidAppear – user3697446 Jun 26 '16 at 16:59

2 Answers2

6

Figured it out basically just need to set .aspectRatio of the window view in viewDidAppear():

override func viewDidAppear() {
    super.viewDidAppear()
    self.view.window?.aspectRatio = NSSize(width: 16, height: 9)
}
user3697446
  • 113
  • 1
  • 9
1

SwiftUI, Xcode 11, Swift 5, macOS Catalina, single window app

For a single-window, SwiftUI macOS app, you can set a fixed aspect ratio to allow proportional resizing for the parent window of your SwiftUI stack by modifying the applicationDidFinishLaunching() in the AppDelegate.swift

 func applicationDidFinishLaunching(_ aNotification: Notification) {
        // …
        // set a fixed aspect ratio of the main window in a macOS app for SwiftUI
        window.contentView?.window?.aspectRatio = NSSize(width: 9, height: 16)
        // continue with your regularly scheduled program…  
        window.makeKeyAndOrderFront(nil)
}
Gary W. Longsine
  • 642
  • 9
  • 12