How to get current orientation of device programmatically in iOS 6?
You can try this, may help you out:
How to change the device orientation programmatically in iOS 6
OR
Objective-c:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]
Swift:
if UIDevice.current.orientation.isLandscape {
// Landscape mode
} else {
// Portrait mode
}
Different ways of getting current interface orientation?
self.interfaceOrientation
returns UIInterfaceOrientation, current orientation of the interface. It is a property in UIViewController, you can access to this one only in UIViewController classes.
[[UIApplication sharedApplication] statusBarOrientation]
returns UIInterfaceOrientation, current orientation of the application's status bar. You can access to that property in any point of your application. My experience shows that it's more effective way to retrieve real interface orientation.
[[UIDevice currentDevice] orientation]
returns UIDeviceOrientation, device orientation. You can access to that property in any point of your application. But note that UIDeviceOrientation is not always UIInterfaceOrientation. For example, when your device is on a plain table you can receive unexpected value.
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.
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")
}
}
Hope this helps anyone else!
Thanks!
How to know current interfaceOrientation in extension of iOS 8?
A UITraitCollection object provides details about the characteristics of a UIViewController object, which manages a set of views that make up a portion of your app’s interface. These characteristics, or traits, define the size class, display scale, and device idiom of the view controller. When a view controller is created, a trait collection is automatically created for that view controller.
You can create and modify a view controller’s trait collection to customize your app. The following methods create a new trait collection containing only the passed parameter:
traitCollectionWithDisplayScale:
traitCollectionWithUserInterfaceIdiom:
traitCollectionWithHorizontalSizeClass:
traitCollectionWithVerticalSizeClass:
Related Topics
Instagram API Prevents Redirection to iOS App Upon Successful Login with 400 Bad Request Error
Does iPhone Support Hardware-Accelerated Aes Encryption
How to Select a Picker View Item in an iOS UI Test in Xcode
Ios7 App Backward Compatible with iOS5 Regarding Unique Identifier
How to Customise Uislider Height
Iterate Over All the Uitablecells Given a Section Id
Keep Bluetooth Sound When Initializing Avaudiosession
iOS Nsnotificationcenter to Check Whether the App Came from Background to Foreground
Trigger a Method in Uiviewcontroller from Its View
How to Launch an App in Foreground When I Enter an Ibeacon Range
Why Is Uiwebview Cangoback=No in iOS7
Core Data Predicate:Unimplemented SQL Generation for Predicate
Finding Distance Between Cllocationcoordinate2D Points
How to Call Https Url in Uiwebview
Cannot Create PDF Document with 400+ Pages on iOS
Uicollectionviewcell Dynamic Height W/Two Dynamic Labels & Auto Layout