Getting iOS device orientation right after the app launch in Swift
In iOS, we can get the physical orientation of the device using the UIDevice class as below:
UIDevice.current.orientation
So to detect if the current device orientation is landscape, you might add a computed property as an extension to UIApplication class as below:
Then you use UIApplication.shared.isLandscape to conveniently check for landscape orientation.
This works perfectly fine with device rotation, however you might get surprised sometimes when it does not work as expected. If you launch your app on landscape orientation, this UIApplication.shared.isLandscape might still give you false value (before rotating your device to portrait and rotating it back to landscape). That’s because the UIDevice.current.orientation value is .unknown right after launching the app.
In that case, to get the correct orientation value, you need to check UIWindowScene’s interfaceOrientation value. If that value is either .landscapeLeft or .landscapeRight, then we can say that the current orientation is landscape.
Looking forward to your suggestions and comments.
#iOS #Swift #
Marketing and Business Strategist: Master of Client Engagement and Optimization | Data-Driven Strategies for Success | Email Marketing Specialist | Operational Efficiency | Lead Generation Specialist
1yInsightful!