how to solve orientation issue in ios7? - ios

I am working on orientation in ios7
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
/**
Changing frame depending on mode
#param interfaceOrientation is UIInterfaceOrientationobject
#param duration is NSTimeInterval object
*/
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self landscapeViewFrame];
}
else if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
[self portraitViewFrame];
}
}
where landscapeViewFrame contains code for landscape View (frames) and portraitViewFrame contains frames for portrait View.These methods used to work, but after upgrading to ios7 they are not responding and not been called, I googled and found these methods are no longer supported.I have tried the other methods
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;//UIInterfaceOrientationIsPortrait(UIInterfaceOrientationMaskPortrait|| UIInterfaceOrientationMaskPortraitUpsideDown);
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
}
else if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft||[self interfaceOrientation] == UIInterfaceOrientationLandscapeRight){
return UIInterfaceOrientationMaskAll;
}
}
but only shouldAutorotate is called the others are not called . Which methods should be replaced in place of those two. Which method should be used/replaced for willAnimateRotationToInterfaceOrientation. please help me.

Related

iPad iOS7 Orientation Problems

I have an app that changes the view when the orientation changes using: [[UIApplication sharedApplication] statusBarOrientation]. On the iPhone, it works but on the iPad, it gives me landscape instead of portrait and portrait instead of landscape.
When I call the getRelevantFrame method outside of the UIDeviceOrientationDidChangeNotification, it will return the correct frame.
It will return the opposite frame (if landscape, will return portrait and vice versa) when responding to the notification.
Both the versions (iPod/iPhone + iPad) uses the same code but this only breaks on the iPad version
Here is the code I used to calculate the relevant frame:
EDIT: Used rob5408's advice. Changed to use UI_USER_INTERFACE_IDIOM().
+ (CGRect)getRelevantFrame {
//Get orientation
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { //iPhone/iPod
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return [self frameiPhoneLandscape];
}
else {
return [self frameiPhonePortrait];
}
} else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { //iPad
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"iPad Landscape");
return [self frameiPadLandscape];
}
else {
NSLog(#"iPad Portrait");
return [self frameiPadPortrait];
}
}
return CGRectMake(0, 0, 0, 0);
}
Here is the notification I used:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
Here is the code I used to read the orientation change:
- (void) didRotate:(NSNotification *)notification
{
NSLog(#"Parent: %#", self.parentController);
if(self.parentController) {
[self setFrame:[TASFrames getRelevantFrame]];
}
}
I transferred the code to a new project. And now it works as it's supposed to. No idea as to why this happens. Code didn't change.

iOS 7 Stop Autorotate

I'm trying to lock orientation of my entire app to portrait modes, so I changed the info.plist, to this:
However, if I rotate the simulator or even my real device, the view still rotates. Any explanation for this? Thanks!
You need to implement this in your ViewController:
For iOS 6 and up:
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
#endif
#endif
For iOS under 6:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL returnBool = YES;
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
returnBool = YES;
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
returnBool = YES;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
returnBool = NO;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
returnBool = NO;
}
else {
returnBool = NO;
}
return returnBool;
}
The pre-processor statements are to make sure the method __IPHONE_OS_VERSION_MAX_ALLOWED exists
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
and the second one:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000
is to make sure the iOS version running on the device is 6 and up because the methods in the pre-processor statements do not exist on versions under iOS 6.
It will crash on iOS under 6 without the pre-processor statements.

How can I make the BarcodeScanner plugin works in the landscape mode?

I am developing a iOS app using Cordova, and I download the Cordova Barcode Scanner Plugin from this link.
However, it only works in portrait mode.
I make some changes in CDVBarcodeScanner.mm.
#pragma mark CDVBarcodeScannerOrientationDelegate
- (BOOL)shouldAutorotate
{
return YES;// NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll; // UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:#selector(shouldAutorotateToInterfaceOrientation:)]) {
return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
return YES;
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
{
// [CATransaction begin];
//
// self.processor.previewLayer.orientation = orientation;
// [self.processor.previewLayer layoutSublayers];
// self.processor.previewLayer.frame = self.view.bounds;
//
// [CATransaction commit];
// [super willAnimateRotationToInterfaceOrientation:orientation duration:duration];
[UIView setAnimationsEnabled:NO];
AVCaptureVideoPreviewLayer* previewLayer = self.processor.previewLayer;
previewLayer.frame = self.view.bounds;
if (orientation == UIInterfaceOrientationLandscapeLeft) {
[previewLayer setOrientation:AVCaptureVideoOrientationLandscapeLeft];
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
[previewLayer setOrientation:AVCaptureVideoOrientationLandscapeRight];
} else if (orientation == UIInterfaceOrientationPortrait) {
[previewLayer setOrientation:AVCaptureVideoOrientationPortrait];
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
[previewLayer setOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
}
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[UIView setAnimationsEnabled:YES];
}
I can rotate to the Landscape mode now, but it still only work in the portrait mode.
How can I fix it?
According to the solution, I delete if (result.empty() && hints.getTryHarder() && image->isRotateSupported()) {} in zxing-all-in-one.cpp.
However, it only works in landscape now.
On zxing-all-in-one.cpp file,
change
if (result.empty() && hints.getTryHarder() && image->isRotateSupported()) {}
to
if (result.empty()) {}
You can also modify the CDVBarcodeScanner.mm. Just replace all ***Portraits with LandscapeLeft/LandscapeRight -> I prefer LandscapeLeft.

How to provide two different views in portrait and landscape?

Im currently working with a IPhone-app that has a "timetable".
in portrait i want it to have a regular table-view with some customization! When i have the IPhone in landscape i want it to change to a more "timetable"-view, with tables and rows.
Is it possible?
Try This
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}
-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//write code for portrait mode
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//write code for landscape mode
}
}
See Apples documentation Creating an Alternate Landscape Interface
Make sure to read the documentation, but here is the code from the example implementation:
#implementation PortraitViewController
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
Answer is Yes it's possible through the following method:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
\\ your timetable customisation goes here
}
}
You also need this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

user enable/disable rotation

I want to allow the interface to change only if the flag enableRotation=1. I’ve been messing around with all the rotation methods and am getting nowhere.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
enableRotation=0;
currentOrientation=fromInterfaceOrientation;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
NSLog(#"enable rotation call %i\n",enableRotation);
if(enableRotation)
{
if ((interfaceOrientation== UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
||(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
)
{
return YES;
}
}
else if (interfaceOrientation==currentOrientation){
return YES;
}
return NO;
}
This is what I’m using to display the alternate view....
- (void)orientationChanged:(NSNotification *)notification
{
if (UIDeviceOrientationIsPortrait(deviceOrientation))
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = NO;
// enableRotation=0;
NSLog(#"Rotation disabled\n");
}
else if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = YES;
// enableRotation=0;
NSLog(#"Rotation disabled Change to Landscape\n");
}
}
You should get rid of the !isShowingLandscapeView condition. That could cause an issue because you may receive that notification multiple times. Actually get rid of the boolean altogether. You don't need it. You can always get the orientation with [UIDevice currentDevice].orientation or [UIApplication sharedApplication].statusBarOrientation
In fact, you do not need to register for this notification period. You could just use -(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration to change the view appropriately.
Furthermore, you should make this change as well:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(enableRotation)
return YES;
else
return NO;
}

Resources