iOS 7 Stop Autorotate - ios

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.

Related

How to disable specific orientation in iOS

I want to disable landscape orientation in some view.
I have overridden the following two methods but these method is not going to call at any time.
- (NSUInteger) supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
Am I missing something?
Please help me.
Create subclass of UINavigationController and override these methods as per below:
- (NSUInteger)supportedInterfaceOrientations
{
if([self.topViewController respondsToSelector:#selector(supportedInterfaceOrientationsForThisContorller)])
{
return(NSInteger)[self.topViewController performSelector:#selector(supportedInterfaceOrientationsForThisContorller) withObject:nil];
}
return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotate
{
if([self.visibleViewController respondsToSelector:#selector(shouldAutorotateNow)])
{
BOOL autoRotate = (BOOL)[self.visibleViewController
performSelector:#selector(shouldAutorotateNow)
withObject:nil];
return autoRotate;
}
return YES;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
and use below method in your UIViewController class where you want to set only portrait orientation
- (NSUInteger) supportedInterfaceOrientationsForThisContorller {
// Return a bitmask of supported orientations. If you need more,
// use bitwise or (see the commented return).
return UIInterfaceOrientationMaskPortrait;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
-(BOOL)shouldAutorotateNow
{
return YES;
}
Check out this answer. The guy has probably explained the delegates in much detail.
Also try,
- (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;
}
}
Did you used below method in your code, with out this supportedInterfaceOrientations will not get called
-(BOOL) shouldAutorotate
{
return YES;
}

Record video in Landscape always even device rotating (Landscape->Portrait->Landscape)

I need to record video always in landscape mode even my device rotating (Portrait->Landscape->Portrait ).
To take angle is simple, im using hyroscope. My problem is to record video in landscape always. Any advices needed.
Im using AVCaptureSession to video rec. AVMutablecomposition? I dont know what to use...
help me,please
Why not forcing your Controller to be only in landscape mode ?
You can do it like this :
- (BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
You can try this code
#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;
}
}
Also have look on the Apple's docs HERE for detail rotation implementations.
Try this before starting to record a video:
let videoDataOuputConnection = videoFileOutput.connection(with: .video)
videoDataOuputConnection!.videoOrientation = AVCaptureVideoOrientation.landscapeLeft

how to solve orientation issue in ios7?

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.

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.

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