Screen orientation on iOS
Apps should adapt to the preferred display orientation of the user. Most apps restrict the screen to portrait orientation, but they should also support landscape orientation. Some users have their devices mounted in a fixed orientation (e.g. on the arm of a wheelchair). Therefore, apps need to support both orientations.
On iOS, make sure all UIInterfaceOrientationMask
values are used for the UISupportedInterfaceOrientations
key inside the Info.plist
file.
To listen to orientation changes, subscribe to orientationDidChangeNotification
. Check the device orientation using UIDevice.current.orientation
.
private var subscriptions = Set<AnyCancellable>()
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter
.default
.publisher(for: UIDevice.orientationDidChangeNotification)
.sink { [weak self] _ in
if (UIDevice.current.orientation.isLandscape) {
// Landscape logic
} else {
// Portrait logic
}
}
.store(in: &subscriptions)
}