[iOS] How to force landscape mode in fullscreen

Our BasicFulscreenHandling sample app showcases how to react to the player switching to fullscreen mode, or change the default behaviour by, for instance, making the player switch to fullscreen upon screen rotation.

A pretty common use case is to force the player to switch to lanscape display upon the user triggering the fullscreen mode.

In order to achieve this, you can simply override the updateLayoutOnFullscreenChange function as below :

private func updateLayoutOnFullscreenChange(fullscreen: Bool) {
    self.fullscreen = fullscreen

    // Update navigation bar
    navigationController?.setNavigationBarHidden(fullscreen, animated: true)

    // Force Orientation of the Application when entering or exiting fullscreen
    let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
    if fullscreen {
        if #available(iOS 16.0, *) {
            windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscape))
        } else {
            // Fallback on earlier versions
        }
    } else {
        if #available(iOS 16.0, *) {
            windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait))
        } else {
            // Fallback on earlier versions
        }
    }

    playerView?.willRotate()
}

This method only works for iOS 16 and above.
For iOS 15 and below, you can try to replace // Fallback on earlier versions with what the following post suggests.

2 Likes