Orientation issue in iPad ios6 - ios

I check the orientation of the device in viewDidAppear and viewWillAppear and force the orientation by calling willAnimateRotationToInterfaceOrientation method.
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
_levelComplete = YES;
[self willAnimateRotationToInterfaceOrientation:[[UIDevice currentDevice] orientation] duration:0.01];
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight) )
{
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
}
}
The problem i face is that toInterfaceOrientation is remains 0 for both viewDidAppear and viewWillAppear method hence program crashes.
What might be the problem?
Please Help!

Try this
- (void) viewDidLoad
{
_levelComplete = YES;
[self adjustViewsForOrientation:self.interfaceOrientation];
}
-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
else
{
}
}

As you mentioned : The problem i face is that toInterfaceOrientation is remains 0 for both viewDidAppear and viewWillAppear method hence program crashes.
The orientation 0 refers to unknown orientation. Set the break point on viewWillAppear and viewDiddAppear and on orientation delegates. Note that do the delagates of orientation gets called first of the view delegates.
You can use the delegate
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
Using this delegate and the application will be present in Landscape mode or any desired orientation mode.
Try it.

Related

change custom label and button to landscape in xcode 5

i create a custom button and label in portrait mode and landscape mode separately as below:
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait) {
[self addButton1];
[self addLabel1];
}
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
[self addButton2];
[self addLabel2];
}
return YES;
}
if i run this code,in landscape mode both buttons and labels are displayed.
if i try this:
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
[self addButton2];
[self addLabel2];
[custombtn1 setHidden:YES];
}
error:use of undeclared identifier.
can somebody help me?
You need to implement the method:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
This method is called regardless of whether your code performs one-step or two-step rotations.
Parameters
fromInterfaceOrientation
The old orientation of the user interface. For possible values, see UIInterfaceOrientation.
Additionally, you could hide unnecessary buttons.
addButton2.hidden = YES;

Device Orientation not getting.

Currently i am working on IOS 7 and my problem is i am not able to get orientation of current device in ViewDidLoad method.
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAll);
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)){
NSLog(#"i am in landscape mode");
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
NSLog(#"i am in portrait mode");
}
}
I write this code in my FirstViewController.m,and when i run my application no condition become true out of them,
can anybody help me why no condition become true even i run my application in portrait mode.
Thanks in advance.
Try this.
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsLandscape(orientation)) {
//Landscape mode
}
else
{
//Portrait mode
}
EDIT : Use UIInterfaceOrientationIsLandscape and UIInterfaceOrientationIsPortrait method to find orientation mode
try this
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIDeviceOrientationIsLandscape(orientation))
{
NSLog(#"i am in landscape mode");
}
if (UIDeviceOrientationIsPortrait(orientation)){
NSLog(#"i am in portrait mode");
}
oky u hav other orientation call back methods this one called before rotation is going to occur - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration and this one just after second one - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation, for example
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
UIInterfaceOrientation orientation = toInterfaceOrientation; //hear toInterfaceOrientation contains the which orientation is device is turning to
if (UIDeviceOrientationIsLandscape(orientation))
{
NSLog(#"i am in landscape mode");
}
if (UIDeviceOrientationIsPortrait(orientation)){
NSLog(#"i am in portrait mode");
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIInterfaceOrientation orientation = fromInterfaceOrientation; // hear fromInterfaceOrientation contains previous state of the orientation
if (UIDeviceOrientationIsLandscape(orientation))
{
NSLog(#"i am in landscape mode");
}
if (UIDeviceOrientationIsPortrait(orientation)){
NSLog(#"i am in portrait mode");
}
}
for more information see docs
hope this helps u .. :)

Use camera in Landscape only app -iOS

I have an application that supports only landscape mode. When I opening the camera the app crashes. I solved the problem by using the following code.
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate {
return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait;
}
But the app turns to support all orientations. All I want is the app only support landscape mode and also uses camera.
Any help will be appreciated.
I finally figured it out. The shouldAutorotate never gets called in ViewControllers. Because the topmost view controller has to return the interface orientations it wants to rotate to. In my case, that's the UINavigationController. So I creates the new category for UINavigationController to make it work. I uses the following methods in the category
#implementation UINavigationController (BugiOSFix)
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate {
return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait;
}
#end
Write this For orientations .........
-(void)viewWillAppear:(BOOL)animated
{
[self willAnimateRotationToInterfaceOrientation:
[UIApplication sharedApplication].statusBarOrientation duration:1.0];
UIInterfaceOrientation orientation =
[UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeRight ||
orientation == UIInterfaceOrientationLandscapeLeft)
{
[self setFrameForLeftAndRightOrientation];
}
else if(orientation == UIInterfaceOrientationPortrait )
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self setFrameForLeftAndRightOrientation];
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
-(void)setFrameForLeftAndRightOrientation
{
if(IS_IPAD)
{
// Write here for left and right orientation
}
else if(is_iPhone)
{
// Write here for iPhone land scape ;
}
-(void)setFrameForPortraitAndUpsideDownOrientation
{
if(IS_IPAD)
{
// Write here for Portrait orientation for iPad
}
else if(is_iPhone)
{
// Write here for iPhone Portrait orientation ;
}
}
See my answer at How to create a landscape-view only ios application for a similar question. All you have to do is to implement one method and your app will be forced to autorotate to the direction you want it.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
This will force your app to rotate to the UIInterfaceOrientationLandscapeLeft direction by providing your ViewController with only one preferred orientation.

Restricting an application to a specific orientation on IOS 5

I have an IOS application developed using Cocos2dx engine.
The application is locked for a specific orientation (for example portrait)
everything in the application seems to work fine and according to the right orientation except for the recent apps bar and notifications which are according to the device orientation.
I want to be able to restrict it so it will have the same orientation as the application itself.
I noticed that removing the landscape orientation inside the info.plist file does the job, but I want to be able to do it through code.
In IOS 6 I found that all I had to was to override referredInterfaceOrientationForPresentation in my RootViewController
and give the right orientation and this does the trick,
but this method doesn't work for IOS 5 and below.
What do I need to do to make this work on devices with IOS 5 and below?
This is the code for the RootViewController (I didn't write it, I just added the last method and I'm trying to figure out how to fix the notifications and recent apps problem)
#include "cocos2d.h"
#import "RootViewController.h"
#import "AppDelegate.h"
#import "misc/deviceOrientation.h"
#import "services/ios/ConfigurationServiceImpl.h"
#implementation RootViewController
#synthesize progressView;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
-(NSUInteger)supportedInterfaceOrientations{
ConfigurationServiceImpl* configurationService = [ConfigurationServiceImpl instance];
if ([configurationService isLandscape])
return UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
-(BOOL)shouldAutorotate{
return YES;
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// printf("shouldAutorotateToInterfaceOrientation\n");
//
// There are 2 ways to support auto-rotation:
// - The OpenGL / cocos2d way
// - Faster, but doesn't rotate the UIKit objects
// - The ViewController way
// - A bit slower, but the UiKit objects are placed in the right place
//
#if GAME_AUTOROTATION==kGameAutorotationNone
//
// EAGLView won't be autorotated.
// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
//
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#elif GAME_AUTOROTATION==kGameAutorotationCCDirector
//
// EAGLView will be rotated by cocos2d
//
// Sample: Autorotate only in landscape mode
//
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
} else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
}
}
// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
//
// EAGLView will be rotated by the UIViewController
//
// Sample: Autorotate only in landscpe mode
//
// return YES for the supported orientations
ConfigurationServiceImpl* configurationService = [ConfigurationServiceImpl instance];
return [configurationService shouldAutorotateToInterfaceOrientation: interfaceOrientation];
#else
#error Unknown value in GAME_AUTOROTATION
#endif // GAME_AUTOROTATION
// Shold not happen
return NO;
}
//This callback only will be called when GAME_AUTOROTATION == kGameAutorotationUIViewController
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
CDeviceOrientation::setDeviceOrientation(CDeviceOrientation::left);
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
CDeviceOrientation::setDeviceOrientation(CDeviceOrientation::right);
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
CDeviceOrientation::setDeviceOrientation(CDeviceOrientation::down);
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
CDeviceOrientation::setDeviceOrientation(CDeviceOrientation::up);
}
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
ConfigurationServiceImpl* configurationService = [ConfigurationServiceImpl instance];
if ([configurationService isLandscape])
return UIInterfaceOrientationLandscapeRight;
else
return UIInterfaceOrientationPortrait;
}
#end
Use shouldAutorotateToInterfaceOrientation for iOS 5 and shouldAutorotate for iOS 6. In iOS5 method, use if case for your supported orientations and return YES for them. In your app summary, enable all the orientations. Hope these will help you.
I make this code skeleton for dealing with wanted & unwanted devices orientations, in my case i want to ignore the UIDeviceOrientationUnknown, UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown, caching the last allowed orientation. This code deals with iPhone and iPad devices and can be useful for you.
- (void)modXibFromRotation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSString *device = [[UIDevice currentDevice]localizedModel];
UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];
if ([device isEqualToString:#"iPad"]) {
if (orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown) {
orientation = (UIDeviceOrientation)cachedOrientation;
}
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
/* Your code */
}
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
/* Your code */
}
}
if ([device isEqualToString:#"iPhone"] || [device isEqualToString:#"iPod"]) {
if (orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown) {
orientation = (UIDeviceOrientation)cachedOrientation;
}
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
/* Your code */
}
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
/* Your code */
}
}
}
Yo have to call this method from two places in your view controller:
Fist begin generating device Orientation Notifications in your App delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//**** ADD THIS CODE *****
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
// Add the main view controller's view to the window and display.
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
then listen for device Orientation Notifications in your view controller:
- (void)viewDidLoad {
[notificationCent addObserver:self
selector:#selector(modXibFromRotation)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
Finally call the modXibFromRotation method from:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear];
[self modXibFromRotation];
}
This will call the check orientation method before the view is shown.

issue with orientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
[self isPortraitSplash];
}
else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
[self isLandScapeSplash];
}
return YES;
}
In my methods isPortraitSplash and isLandScapeSplash, i am setting the frames for the view.
When orientation changes, it's always calling isLandScapeSplash - not able to call isPortraitSplash method.
Can any one advise me why this is happening?
Your existing if statement is comparing a BOOL to a UIDeviceOrientation. Your test needs to be:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
[self isPotraitSplash];
}
else if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
[self islandScapeSplash];
}
return YES;
}
UIInterfaceOrientationIsPortrait returns a BOOL, so that's all you need in your if statement condition.
Update: I will also add that I agree with the other answers that it's better to do this work in willRotateToInterfaceOrientation:duration:, instead of shouldAutorotateToInterfaceOrientation:.
But, that isn't the reason your original code was failing. The original code was failing because of the if test comparing the UIDeviceOrientation to the BOOL.
Use - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration instead of shouldAutorotateToInterfaceOrientation, it is guaranteed to be called before a rotation occurs.
Do not remove shouldAutorotateToInterfaceOrientation, return YES for every orientation you want to support.
First of all in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
you have to declare which all orientations you want to support.
and in
- (BOOL)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
[self isPotraitSplash];
}
else if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
[self islandScapeSplash];
}
}
you have to set the frames or any other for layout changes, and use like above.

Resources