How to disable any autorotation? - iphone

I am trying to code for calling Landscape screen from Portrait screen.
When current screen orientation is Landscape, it would be doesn't allow any autorotation.
I've tried the following code :
// Override to allow orientations other than the default portrait orientation.
/*- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}*/
- (void)viewDidLoad {
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, +90.0, +90.0);
[self.view setTransform:landscapeTransform];
}
But above code is allowing UIInterfaceOrientationLandscapeRight.
How to disable any autorotation ?
Thanks in advance.

Do you want start your application in the landscape mode or not?
In all case you must override the shouldAutorotateToInterfaceOrientation method.
But if you want start your application in the Portrait mode and then go to the Landscape mode you can add a static boolean to know if you must stay in the landspace mode or not.
You code must look something like that:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(isInPortraitMode) {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
} else {
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
isInPortraitMode = YES;
}
return YES;
}
}
- (void)viewDidLoad {
isInPortraitMode = NO;
//...
}

Related

Only statusbar is rotating to Landscape (iPhone)

In One of my Viewcontroller that supports only Portrait orientation. On my
device when rotate the status bar is rotating to landscape
orientation. The rest of the view remains in Portrait mode. I want to prevent the roation of statusbar. This
problem is happening only in IOS7
// Overridden function to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
// iPhone: support only upright portrait
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
else
{
// iPad: any orientation is OK
return YES;
}
}
// For iOS6.0 Rotate
- (BOOL)shouldAutorotate
{
return NO;
}
Use this method to keep it in Portrait view :
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;

Supported orientation madness

I know this is the most commonly asked question and trust me I have tried every kind of combination to make this happen and went through most posts here on stack overflow.
I have a UINavigationController called v1ViewController and what I am trying to do is when it is shown to the user I just want it to either show in portrait or portrait-up-side-down modes, no landscape even if the user rotates the device in landscape it shouldn't do anything.
NOTE: I did highlight all options in Xcode for supported interface orientations as in one of my other view controller I need to show user something in both landscape and portrait modes but for this UINav Conrtoller I want it to just remain in portrait modes.
The code I have for iOS 5.0 work just fine without a problem.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
return YES;
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
return YES;
}
else
{
return NO;
}
}
the headache I have is with iOS 6. No matter what I try and do it still lets the user rotate the bloody thing in landscape (see code and screenshots)
// *********************
// iPhone 5 orientation support
// *********************
- (BOOL)shouldAutorotate
{
UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
return YES;
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
return YES;
}
else
{
return NO;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
You need to ensure you override the base UINavigationController and assign a custom class to it(not just the viewController that is the subview for the UINavController..ie the view contained below the navigationBar). Inside the UINavigationController, return NO on shouldAutoRotate.

How to get default LandscapeLeft Orientation in iOS5?

Sorry if this question is duplicate, but I can't find the same solution.
In iOS6, if I want to set default orientation for one UIViewController, I just use:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeLeft;
}
But how to do same in iOS5, I test both:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);//not default
}
and
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationMaskLandscapeLeft);// = return NO;
}
But the default orientation always Portrait.Any suggestion?
call the below method where you want to check the status of orientation.happy coding :-)
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self CheckForViewForOrientation:toInterfaceOrientation];
}
- (void) CheckForViewForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Ur lable portrait view
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Ur lable for landscape view
}
}
If you want to achieve this for whole application, change supported orientation UISupportedInterfaceOrientations in app.plist file
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation==UIInterfaceOrientationPortrait) {
// do some
}
return YES;
}
Include both the methods to support both 5 and 6
You need to provide
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
for that single View controller.

iPhone interface orientation change not returning to portrait orientation

So I have a view that I present modally when the interface orientation changes to landscape. However when the orientation returns to portrait and the modal view dismisses itself, the tableview from the initial view remains in landscape orientation (this table view must be only in portrait orientation)
Here is the code :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight) );
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
[self performSegueWithIdentifier:#"showCatChart" sender:self];
}
if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
[self refreshTableView];
}
}
I tried to refresh the tableview but that doesn't make it portrait again ...
this could be from the view hierachy ...
NavigationController->tableView (only portrait)->tableview->landscapeViewModalController
With iOS 6, you need to implement the following:
- (BOOL)shouldAutoRotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
In your AppDelegate, you need to change the following:
[self.window addSubview:viewController.view];
to
[self.window setRootViewController:viewController];
Also, keep your current code if you want to support previous versions of iOS.
Use the following in appdelegate.
[self.window addsubview:viewcontroller];
This alone will solve your orientation problem.

Issue on UIOrientation in ipad

I'm developing the app for all the orientations in Ipad....But during the development I did only for landscape mode..But nw I want to change to all the possible orientations... Here I got some problem....
Here is the code...
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
//return YES;
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || UIInterfaceOrientationPortrait);
}
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
\\Here I did my stuff..
}
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
\\Here I did my stuff..
}
}
The problem is,When the simulator is launched and if itz in the landscape mode,I got the proper size of the views,but if the simulator is in potrait mode I can't get what I want... If I change the orientations of simulator I get the correct size of the views...
This was the same problem that was facing.
And i solved it by doing this:
First change this function:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
and then in the viewDidLoad function you can check whether the device is in landscape or in Portrait.
-(void)viewDidLoad
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
//landscape view code
}
else
{
//portrait view code
}
}
hope this will help