How to check if device orientation is landscape left or right in swift?
you can do something like,
if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft{
}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight{
}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortraitUpsideDown{
}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortrait{
}
SWIFT 5
if UIDevice.current.orientation.isLandscape {
} else if UIDevice.current.orientation.isFlat {
} else if UIDevice.current.orientation.isPortrait {
} else if UIDevice.current.orientation.isValidInterfaceOrientation {
}
SWIFT 3
if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
} else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
} else if UIDevice.current.orientation == UIDeviceOrientation.portrait {
} else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {
}
How to check if device orientation is landscape left vs right in flutter?
Flutter don't give us left or right yet but this library can help you.
iOS Landscape Left vs Right vs Wrong
I think mainly it's a matter of a contradiction between Xcode UI and info.plist. Xcode UI shows "Device Orientation", while info.plist speaks about "Supported Interface Orientation". But as we know those two are different things, so there's definitely something wrong there.
Assuming, between the two, that info.plist wins then those fields (for iPhone and iPad) are used to specify Supported Interface Orientation. I.e. the same option that you can also override in a specific view-controller through supportedInterfaceOrientations
.
Instead the referenced doc page is about Device Orientation, with its own definition of what is landscapeLeft: home button to the right.
Now, looking at Interface Orientation Mask docs, there's no real detail on what is landscapeLeft, but old Xcode UI screenshots show that home button is to the left. See e.g. from this SO thread:
EDIT: Interface Orientation doc page instead has detail on what is landscapeLeft: home button to the left, as shown in old screenshots.
Conclusions
So, all in all, it seems to me that:
Xcode UI/info.plist are about supported interface orientation, which has its own definition of landscape left/right
Device orientation has the opposite definition of landscape left/right
How to check in which position (landscape or portrait) is the iPhone now?
Use the [[UIDevice currentDevice] orientation]
method, as specified here.
How to detect orientation change?
Here's how I got it working:
In AppDelegate.swift
inside the didFinishLaunchingWithOptions
function I put:
NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)
and then inside the AppDelegate class I put the following function:
func rotated() {
if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
print("Landscape")
}
if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
print("Portrait")
}
}
In Swift, how to get the device orientation correctly right after it's launched?
I have tested many times about orientation, so I have summed up some experience.
In all iPhone devices, except iPhone6(s) plus, the only interface orientation is .portrait
. If App is launched in landscape mode, there must be a change of orientation. One will receive the UIDeviceOrientationDidChangeNotification
. It's an appropriate time to get the orientation.
Regarding the launching when in landscape with iPhone6, the orientation after the launch will change once:
The launching when in landscape with iPhone6 plus, after launch the orientation never changed:
Two different screenshot with the same app,
So before the app does change orientation, the orientation is still like in the home page.
In viewDidLoad
, the orientation has not changed yet, the log will be the wrong direction.
Lock the orientation of the app in a specific View Controller in Swift
You can force orientation with few steps:
Firstly, In your AppDelegate
define a orientation property and conform supportedInterfaceOrientationsFor
var orientationLock = UIInterfaceOrientationMask.portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return self.orientationLock
}
Then declare utility struct to set orientation using KVO:
struct AppOrientationUtility {
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation) {
self.lockOrientation(orientation)
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}
}
How to use:
//For portrait
AppOrientationUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)
//For landscape
AppOrientationUtility.lockOrientation(UIInterfaceOrientationMask.landscapeRight, andRotateTo: UIInterfaceOrientation.landscapeRight)
Related Topics
How to Add a Ibaction to a Button Programmatically in Swift 4
3D Touch Quick Actions Not Working at All
Swift, Parse.Com: How to Pass Data from Query
Swift Add Line Above to Control
How to Use a Generic Class Without the Type Argument in Swift
How to Call Presentviewcontroller in Uiview Class
How to Get the Today's and Tomorrow's Date in Swift 4
How to Use Key-Value Coding in Swfit 4.0
Uitableview Inside Uitableviewcell with Dynamic Height
Why Doesn't iOS System Sound Play in Simulator
Get All List of Uiviewcontrollers in iOS Swift
Replaykit Startrecording Sometimes Never Enters Completion Handler
Start/Stop Image View Rotation Animations
Memory Usage Keeps Rising on Older Devices Using Metal
Cannot Convert Value of Type 'String.Type' to Expected Argument Type 'String!'