Until now, I used to use this code to check if my device is in a portrait or landscape mode:
if (UIDeviceOrientationIsPortrait(UIDevice.current.orientation)){
//portrait
}else {
//landscape
}
But I discovered that this variable check the real position of the device in the 3D space. So if my iPad or iPhone is in a portrait mode and I put it on a table, parallel to the ground, the orientation will not be considered as portrait.
Is there a variable or something similar that can I check to know the orientation of the app and not the physical orientation?
Use UIApplication.shared.statusBarOrientation: The current orientation of the app's status bar.
Related
I locked my view in portrait, but for some reason I need to know the actual device orientation, The UIDevice.current.orientation as documentation is saying in description Returns the physical orientation of the device. this var sometimes works well and return real device orientation and sometimes return protrait (which is wrong, because I rotated the device to landscape). So I expect to get real device orientation (despite I locked my view to portrait), is it a wrong expectation? If not why this inconsiste behavior is happening?
Note: I tried UIDevice.current.endGeneratingDeviceOrientationNotifications(), the result is the same.
when portrait orientation lock is on, UIDevice.current.orientation always return .up
so, use motion instead:
iOS device orientation disregarding orientation lock
In my camera app, everything works perfectly fine orientation wise. If portrait orientation lock is not enabled on a users phone, then everything works perfectly.
However, if a user has portrait orientation lock enabled and they record sideways, then the video gets recorded in portrait mode but everything is sideways inside the video.
Is there any way to check if portrait orientation lock is enabled or any way to get around this? Thank you.
There are a couple of things. You can override the shouldAutorotate function in the view controller that you are using the camera in like so:
override func shouldAutorotate() -> Bool {
return true
}
Then you could set it to false again once they are finished with the camera so it retains their device settings. Not sure if this is the best way, but is A way.
Another possible way that you might be able to get around this is to simply always check the device orientation. The device will still detect the physical orientation of the device even if the visual orientation of the UI doesn't autorotate (https://developer.apple.com/documentation/uikit/uidevice/1620053-orientation). So the following shows the possible device orientations, as per the Apple documentation (https://developer.apple.com/documentation/uikit/uideviceorientation)
enum UIDeviceOrientation : Int {
case Unknown
case Portrait // Device oriented vertically, home button on the bottom
case PortraitUpsideDown // Device oriented vertically, home button on the top
case LandscapeLeft // Device oriented horizontally, home button on the right
case LandscapeRight // Device oriented horizontally, home button on the left
case FaceUp // Device oriented flat, face up
case FaceDown // Device oriented flat, face down
}
Knowing those you can just check if the current orientation is one of those:
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft) {
...override autorotate to true
}
These might solve your issue.
You can disable portrait orientation lock by using this method:
- (NSUInteger)lockorientation
{
return UIInterfaceOrientationMaskPortrait;
}
I don't check any device orientation on the Deployment Info page, that is, there is no entry in the Supported interface orientations on the Info.plist. However, the iPhone still can rotate to landscape. How can I disable iPhone rotation? Do I need to write some code in the view controller? Thanks in advance.
When you don't check any orientation, it defaults to all of them. To keep the orientation at only portrait, you must check the portrait only orientation.
You can toggle here in the device orientation section which orientation that you want your app to support:
Just set it to portrait and you'll be fine.
I have made me app so that device orientation is portrait only, however there it still has a place to put landscape launch image. Will I have to have one? If so can I just have a rotated version of the portrait one (i.e. this will be sideways if the device is held in a landscape manner)?
You don't need to have a landscape default image even if you do support landscape.
If you are only in portrait, then a single image will do.
My ipad app only runs in landscape orientation. But when I test on an actual device (not the simulator) it always seem to start in one particular landscape orientation, then turn 180 degrees to match the current physical landscape orientation. So, it seems to start in LandscapeLeft(I think) then rotate 180 if I have it in LandscapeRight. There's nothing in Info.plist to set the initial orientation.
Can anyone suggest a way to detect current orientation before view loads and set it to correct landscape view before showing?
Thanks
Fitto
There is the UIInterfaceOrientation~iPad key which sets the initial interface orientation on startup.
There is also the UISupportedInterfaceOrientations key which informs the app which orientations are supported.
There is no way, however, to make the app start in the current orientation of the iPad.
Have you looked into these questions?
Get current orientation of iPad?
iPad launch orientation
Also, technically, the iPad should just do it. Make sure you have supported orientations set properly, as I'm sure it checks those to see where it should start.