I need to set my camera orientation to landscape in iOS 9; my code works in iOS 7 and 8 but does not seem to work now.
In iOS 8, I call CameraViewController : UIImagePickerController, with setting ShouldAutoRotate >> return NO.
I have already tried this:
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeRight] forKey:#"orientation"];
}
else
{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:#"orientation"];
}
There is no change in UIDeviceOrientationLandscapeRight, but if UIDeviceOrientationLandscapeLeft, it's rotated 90 degrees but the size is not correct.
In the info.plist I have set it to only accept landscape orientation. What else should I do or might have missed?
I am working in an OpenGL ES game, c++ and iOS. I would like that the canvas axes be always as in default portait orientation, and also be able to know which is the initial device orientation and all the orientation changes.
The problem I have is that if I configure the XCode project to support the 4 orientations: 2 Portraits and 2 Landscapes, then the Frame which is embedded the OpenGL canvas has the axes depending the initial orientation. If I configure the project to support only Portrait Orientation, then the Application won't tell me if it is in Landscape.
I would like to find a way to do this as much programmatically as possible.
1) To Know programatically in iOS
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeLeft)
landscapeRight = false;
else if(orientation == UIDeviceOrientationLandscapeRight)
else if(orientation == UIDeviceOrientationPortraitUpsideDown)
else if(orientation == UIDeviceOrientationPortrait)
....
2) For Default portrait view
Restrict other orientations in your Xcode project's info.plist
I have an app that works on landscape, already in the store, and decided to add a camera for a certain new feature in my next update.
with an iPad 2 the image inside the camera appears rotated 90 degrees in clockwise direction.
Once the picture is taken it appears it's saved correct position.
Needed is that always there should be a landscape mode,while taking picture and once image is clicked and saved.But image inside the camera appears to be in Portrait view. I also checked the device orientation i.e faceUp orientation.
I have used the below code to manage the orientation but could not resolve
- (BOOL)shouldAutorotate {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
// UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationFaceUp )
{
return NO;
}
else if (orientation == UIDeviceOrientationFaceDown)
{
return NO;
}
return YES;
}
I have two views in my app; "Calculator" and the "Tape". I can click on the button inside calculator to get to the tape and vice versa. I set the rotation as per code below and it works fine most of the time.
However there are issues if I rotate either calculator view or tape view to landscape and then if I try to access other view, interface is all messed up like it doesn't recognize that device was already rotated. Any suggestions?
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self doLayoutForOrientation:toInterfaceOrientation];
}
- (void)doLayoutForOrientation:(UIInterfaceOrientation)orientation {
if (UIInterfaceOrientationIsPortrait(orientation))
{
//set the frames here
}
else
{
//set the frames here
}
}
write these codes in the view will appear of each view.
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//adjust the view for landscape
}
else if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//adjust the view for portrait.
}
I got this code that if the device is in landscape left/right or upside down it rotates and shows another view controller. but if it´s in the orientation face up or face down, then how can I tell if it´s in landscape mode or portrait? cause I only want to rotate if it´s face up or down and in landscape mode
- (void)viewDidAppear:(BOOL)animated
{
UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation];
NSLog(#"orientation %d", orientation);
if ((orientation == 2) || (orientation == 3) || (orientation == 4))
{
[self performSegueWithIdentifier:#"DisplayLandscapeView" sender:self];
isShowingLandscapeView = YES;
}
}
The interfaceOrientation property is deprecated since iOS 8.
And the helpers methods
UIDeviceOrientationIsPortrait(orientation)
UIDeviceOrientationIsLandscape(orientation)
won't help either, because they return false when orientation is .faceUp.
So I ended checking this way:
extension UIViewController {
var isPortrait: Bool {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait, .portraitUpsideDown:
return true
case .landscapeLeft, .landscapeRight:
return false
default: // unknown or faceUp or faceDown
guard let window = self.view.window else { return false }
return window.frame.size.width < window.frame.size.height
}
}
}
This is in a UIViewController extension, so I can revert to comparing the screen width and height if everything else fails.
I use window because if the current ViewController is embedded in a container, it may not reflect the global iPad orientation.
In UI code you usually should not depend on the device orientation but the user interface orientation. There's often a difference between them, for example when a view controller only supports portrait.
The most important difference for your case is that the interface orientation is never face up/down.
In your case you can just ask the view controller for the current user interface orientation: self.interfaceOrientation.
Your condition could be expressed somewhat like if (deviceOrientation is face up/down and interfaceOrientation is landscape)
Bear in mind that a device orientation landscape left means a user interface orientation landscape right.
Yes you can, the UIDeviceOrientation is an enum which contains:
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
There are even two helpers:
UIDeviceOrientationIsPortrait(orientation)
UIDeviceOrientationIsLandscape(orientation)
Just cmd on UIDeviceOrientation to show the headerfile where the enum is declared.
There is one way to check it.
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
Use the first one to check if the device is in faceup and second one will tell you if the device is in portrait or landscape.