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;
}
Related
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.
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
I have a project whose rootViewController is a UINavigationController, and in my project only the view controller loading a full screen UIWebView. I need to change its orientation.
I have finished fixing the issue, but there's some trouble: When a user is in the view controller with the web view in the landscape orientation then clicks the back button, the previous view controller is also in landscape orientation. I want it to be in portrait, however. Any ideas on how to do this?
If you can read Chinese, I found this page, thanks.
implement
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
for whatever viewcontroller you require to stay in portrait.
I have try many solution, but the correct answer with working solution is:
ios 8 and 9, no need to edit info.plist.
- (BOOL) shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
possible orientation
UIInterfaceOrientationUnknown
The orientation of the device cannot be determined.
UIInterfaceOrientationPortrait
The device is in portrait mode, with the device held upright and the home button on the bottom.
UIInterfaceOrientationPortraitUpsideDown
The device is in portrait mode but upside down, with the device held upright and the home button at the top.
UIInterfaceOrientationLandscapeLeft
The device is in landscape mode, with the device held upright and the home button on the left side.
UIInterfaceOrientationLandscapeRight
The device is in landscape mode, with the device held upright and the home button on the right side.
This question is a little strange. In Adobe AIR I am trying to combine an iPhone and an iPad version of my app into one single app. However i have a hard time dealing with orientation. I need the app on iPad to appear in landscape mode and on the iPhone it should appear in portrait mode.
Can this be done?
in shouldAutorotateToInterfaceOrientation do this
But don't forget to adjust view accordingly (set appropriate frames to subView)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
else
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
The first problem is detecting whether the device is an iPad or iPhone. I believe the only way to do this is by checking the device resolution (of course, you can distribute separate binaries for the different devices, but you mentioned doing this in one app).
Usually you can handle screen rotation in the XML app descriptor, e.g.:
<aspectRatio>landscape</aspectRatio>
<autoOrients>false</autoOrients>
However, since you want to do different actions depending on the device, this won't work.
An alternative that I have used in the past is to detect Orientation events and prevent the default and manually set the orientation through AS3. Here is a good example of how to do this: AS3 - iOS force landscape mode only?
Using the code sample presented there, it's just a matter of taking your device detection / screen resolution detection code and setting the screen orientation to the appropriate detection for that device (and preventing the appropriate orientation events for that device).
In case you still need it, I made the adaptation of the "force landscape mode only" to do the same with portrait only. Here's the code:
var startOrientation:String = stage.orientation;
if ( startOrientation == StageOrientation.ROTATED_RIGHT)
{
stage.setOrientation(StageOrientation.UPSIDE_DOWN);
}else if(startOrientation == StageOrientation.ROTATED_LEFT){
StageOrientation.DEFAULT;
}
else
{
stage.setOrientation(startOrientation);
}
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChangeListener);
function orientationChangeListener(e:StageOrientationEvent)
{
if (e.afterOrientation == StageOrientation.ROTATED_RIGHT || e.afterOrientation == StageOrientation.ROTATED_LEFT )
{
e.preventDefault();
}
}
I have a pretty standard iPad application that is setup to only be landscape. To affect this, I have set the initial interface orientation to landscape, and the support interface orientations to just the single landscape left home button, and also overridden shouldAutorotateToInterfaceOrientation properties to the following:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL b = UIInterfaceOrientationIsLandscape(interfaceOrientation);
return b;
}
The really odd thing is that when the app starts out, it is correct, and rotating the iPad upside down does nothing, but rotating with the home button down rotates the screen, but once rotated, it will never rotate back, so thinking this is something other than rotation settings.
Has anyone run into this before?
From the docs:
#define UIInterfaceOrientationIsLandscape(orientation) \
((orientation) == UIInterfaceOrientationLandscapeLeft || \
(orientation) == UIInterfaceOrientationLandscapeRight)
So if you want to support just ONE of the landscape rotations, this is not the way to go...