Handle orientation changes

Learn how to update your custom player layout when the device orientation changes.

When building a custom player UI, you need to handle orientation transitions to keep your controls properly positioned in both portrait and landscape modes.

Update layout on rotation

Override viewWillTransition(to:with:) to adjust your player constraints during rotation:

1override func viewWillTransition(
2 to size: CGSize,
3 with coordinator: UIViewControllerTransitionCoordinator
4) {
5 super.viewWillTransition(to: size, with: coordinator)
6
7 coordinator.animate(alongsideTransition: { _ in
8 self.updatePlayPauseConstraintsForOrientation()
9 self.view.layoutIfNeeded()
10 })
11}

Detect the current orientation

Use the following helper to check whether the device is in landscape mode:

1private func isLandscapeMode() -> Bool {
2 if #available(iOS 13.0, *) {
3 let orientation = view.window?.windowScene?.interfaceOrientation
4 return orientation == .landscapeLeft || orientation == .landscapeRight
5 } else {
6 return UIApplication.shared.statusBarOrientation.isLandscape
7 }
8}

Use this helper to adjust control positions, padding, or layout constraints based on the current orientation.