IPad Camera Orientation issue - objective-c

I am implementing an Application Regarding to Camera Mode Functionality But App Run Only Landscape But the Camera Opens in Some part of the IPad Only Remaining is in Black.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
camPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
camPicker.delegate = self;
camPicker.allowsEditing = NO;
[self presentModalViewController:camPicker animated:YES];
For Orientations Fixed as
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
}else
{
return interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

Set orientation as:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

Related

iOS8 Modal ViewController not fires shouldAutoRotate.

I have a parentViewController that calls a modal ViewController with Delegate UITableViewController. The problem is that I do not have navigation stack to get the value from the parent's shouldAUtoRotate.
Since the shouldAutorotate never fired it gets it from a main controller where return NO and this cannot be changed. Is there a way to manipulate the modal controller to get the proper shouldAutorotate set to YES?
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self.pv dismiss:YES];
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
//[self fullScreenAction:nil];
} else {
// [self exitFullScreenAction:nil];
}
}
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
} else {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
//ios >=6
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ||
(interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
I managed to solve my problem although is not the best solution, maybe for some will be useful.
So i create a new Category Controller in order to use it to set manually the Rotation calls.
.m
#import "UINavigationController+AutoRotate.h"
#implementation UINavigationController (AutoRotate)
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationMaskAll;
}
#end
and .h
#import <UIKit/UIKit.h>
#interface UINavigationController (AutoRotate)
- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
#end
And then the Modal was working as it should without a navigation controller.

Force controllers to Change their orientation in Either Portrait or Landscape

I have following Controllers, (I have selected all types orientation modes in iPad)
Here is my iPad Storyboard layout
Custom NavigationController > Loading Ctrl > Main Controller.
My Custom Navigation Contains
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
In my Loading Controller
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM())
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
else
return UIInterfaceOrientationMaskPortrait;
}
The supportedInterfaceOrientations gets called as usual and everything seems ok, But when I push my Main Controller using performSegue
-(NSUInteger)supportedInterfaceOrientations
{
if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM())
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
else
return UIInterfaceOrientationMaskPortrait;
}
No more calls in MainController. Why is that?
There is one trick.
Fetch the status bar from the Application and rotate it.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
Create and display and dismiss an empty view controller modally.
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
Now your device shoud have been forced to rotate. You could now segue to a proper view controller or push one using the navigation controller.
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIInterfaceOrientationPortrait);//choose portrait or landscape
}
- (BOOL) shouldAutorotate{
return NO;
}
- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;//choose portrait or landscape, same as above
}

Error in iOS 6 after adding GameCenter to a landscape-only cocos2d app

I'm having the issue described here:
https://devforums.apple.com/thread/165384?tstart=0
Where my app crashes trying to load the GameCenter login screen because the screen is in portrait and my app only supports landscape.
I've tried every solution described in the above thread, as well as all the solutions on the following thread:
Crash on presenting UIImagePickerController under iOS 6.0
And here:
http://www.cocos2d-iphone.org/forum/topic/36639
None of the solutions work. Either the crash still occurs, or the login works fine, and either my app then freely rotates between landscape and portrait, or it locks itself into portrait and screws up the entire UI.
What I want is for the GameCenter login to work in portrait, and then for everything else in the app to occur in landscape.
Here are all the rotation methods contained in my app. These are the ones from the myNavigationController implementation in appdelegate.m:
-(NSUInteger)supportedInterfaceOrientations {
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationMaskLandscape;
// iPad only
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
// iPad only
// iPhone only
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
-(BOOL)shouldAutorotate{
return NO;
}
And from the AppController implementation in appDelegate.m:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Contained in RootViewController.m:
-(BOOL)shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
#if GAME_AUTOROTATION==kGameAutorotationNone
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#elif GAME_AUTOROTATION==kGameAutorotationCCDirector
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
} else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
}
#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
#else
#error Unknown value in GAME_AUTOROTATION
#endif // GAME_AUTOROTATION
// Shold not happen
return NO;
}
kGameAutorotationUIViewController
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect = CGRectZero;
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
}
CCDirector *director = [CCDirector sharedDirector];
UIView *glView = [[CCDirector sharedDirector] view];;
float contentScaleFactor = [director contentScaleFactor];
if( contentScaleFactor != 1 ) {
rect.size.width *= contentScaleFactor;
rect.size.height *= contentScaleFactor;
}
glView.frame = rect;
}
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController
Just answered this question a few days ago here:
Cocos 2d 2.0 shouldAutorotate not working?
There are instructions on what you need to do to get this to work in that answer. Hope this helps!

Screen Orientation for Portrait and Landscape Orientation in iOS (Objective-C)

I have a situation where I want to call separate method for different Orientation (Portrait and Landscape).
For example:
If (Orientation == Portrait)
{
Some method a;
}
Elseif (Orientation == Landscape)
{
Some method b;
}
I use [[UIApplication sharedApplication] statusBarOrientation] to know the orientation.
then I use this method
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
//Do something if landscape
} else {
//Do something in portrait
}
don't use [[UIDevice currentDevice] orientation] because it doesn't work properly if the device is on a table, for example.
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}
UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait are the macro's.
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
if(deviceOrientation == UIDeviceOrientationPortrait)
...
else ...
the enum is UIDeviceOrientation[something]
TRY THIS:
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{// perform ua operation for landscape mode
} else{
//perform ua operation for portrait mode
};
Objective-c:
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
if (UIDevice.currentDevice.orientation == UIInterfaceOrientationPortrait) {
printf("Portrait");
} else {
printf("Landscape");
}
}

iOS - Change View with the device orientation

I'm using iOS5 and storyboard. I have two view with two different identifier.
I want change the view when change the orientation of the device.
This is my code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
self.view = [self.storyboard instantiateViewControllerWithIdentifier:#"Landscape"];
}
else if(((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
self.view = [self.storyboard instantiateViewControllerWithIdentifier:#"Portrait"];
}
return YES;
}
But the app crash.
Which is the problem?
EDIT:
If I add two uiview and I put the two id to the uiview when I rotate the device It crash.
This is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void) orientationChanged:(id)object
{
portraitView = [self.storyboard instantiateViewControllerWithIdentifier:#"TermoregolatoreTop"];
landscapeView = [self.storyboard instantiateViewControllerWithIdentifier:#"Landscape"];
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portraitView;
}
else
{
self.view = self.landscapeView;
}
}
self.view = [self.storyboard instantiateViewControllerWithIdentifier:#"Portrait"];
Will return an id to a UIViewController and you are assigning it to a UIView.
You have to remove the observer on UIDeviceOrientationDidChangeNotification when releasing the view controller.
The crash happens because that notification is triggered on the class which most likely has been removed from memory.