having issue with shouldAutorotateToInterfaceOrientation - iphone

I have the following code in my UIViewController and I am testing for iOS 5 in a device and both simultor.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (IS_IPHONE){
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
} else {
return YES;
}
}
I put a breakpoint and it is indeed getting called, however it still rotates to landscape. Why is this?

Because you told it to do so. return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown; means that you want to autorotate to every direction except portrait upside-down, and that includes landscape. For iPad, even this constraint is missing, so it will autrotate to any orientation.
(You should have a fresh breath of documentation...)

Use this instead
return (interfaceOrientation == UIInterfaceOrientationPortrait);

Related

Disable Landscape Orientation in IOS5

I want to eliminate Landscape Orientation in my application, which is built for IOS 5. In IOS 6 I know this is possible - but it doesn't seem to be working for me in the earlier version.
I am setting only two orientations in my plist file (Portrait w/Home Button on Top and Portrait w/Home Button on Bottom). Regardless, Landscape still crops up in IOS 5.
Is there something further I need to do to eliminate this from happening?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
// return (interfaceOrientation == UIInterfaceOrientationPortrait);
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
return NO;
}
else
{
return YES;
}
}
write this code in .m file for ios5 orientation
let me know it is working or not....
Happy Coding!!!!
This will work. Go to storyboard deselect the orientation you don't want and in view controller, write this code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation==UIInterfaceOrientationPortrait );
}

iOS6 Device Rotation To Be Restricted

For my app, I want to let the device rotate anyway but upside-down. This is working fine. However, I want to stop the app from rotating specifically from
landscape left -> landscape right - and vice versa
If anyone is curious, this is because that rotation messes up my layouts, as they each rotate from a common point
My code for iOS 5, which I think would work, is like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
NSLog(#"Rotating");
if((lastOrient == 3 && toInterfaceOrientation == 4) || (lastOrient == 4 && toInterfaceOrientation == 3)){
lastOrient = toInterfaceOrientation;
return NO;
}
lastOrient = toInterfaceOrientation;
return YES;
}
Where 3= landscape left and 4= landscape right
Any suggestions on how to do this with iOS6? Or a completely different solution?
shouldAutorotateToInterfaceOrientation is deprecated in ios6. Use this:
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (lastOrientation==UIInterfaceOrientationPortrait && orientation == UIInterfaceOrientationPortrait) {
return NO;
}
return YES;
}
Haven't tested this code. You can get more info on these posts:
shouldAutorotateToInterfaceOrientation is not working in iOS 6
shouldAutorotateToInterfaceOrientation not being called in iOS 6
Ok I answered my own question here:
The good news is, there is definitely a way to do this! So heres the basics:
In iOS6, it is up to the appDelegate to handle whether the app can rotate in general. Then, when the device gets a rotation signal, it will ask your view for its supported orientations. This is where I implemented my code. In fact, shouldAutorotate() plays no role in the solution.
So I create a variable to keep track of the last orientation, and change it in
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
This way I can compare orientations
-(NSUInteger)supportedInterfaceOrientations
{
NSLog(#"Last Orient = %d", lastOrient);
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if (lastOrient != 3 && lastOrient != 4) {
NSLog(#"All good, rotate anywhere");
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else if(lastOrient == 3){
orientations |= UIInterfaceOrientationMaskLandscapeRight;
NSLog(#"Can only rotate right");
}
else if(lastOrient == 4){
orientations |= UIInterfaceOrientationMaskLandscapeLeft;
NSLog(#"Can only rotate left");
}
return orientations;
}
Seems to work for me. Slightly a hack, but it does what it needs to do

Force horizontal orientation for custom camera overlay

I am trying to force orientation on my custom camera overlay, but I am struggling to get it to work, I have tried this,
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
but that does not seem to work anymore since Xcode updated to version 4.5.
Also, what would be the right way to force horizontal orientation on a single view, as I understand it the above is not best practice?
For ios6, need to do the following things.
(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}

Any idea why this view is not rotating?

I'm working on a photo view controller to display some photos in portrait and landscape view as well. What I did is edit the -(BOOL)shouldAutorotateToInterfaceOrientation orientation with the code below but when testing in xcode (menu hardware > Rotate right) the view does not rotate in landscape. Any thing wrong in what I did ?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
The view controller is a part of a tab based iphone app and this view is not root view: is this the problem ?
Also check your supported Orientations. For XCode 4 (Project->Summary(Tab)->Supported Device Orientation->Desired Orientations).
My first idea would be to just return true; to make sure there's no issue with the parameters being passed in / your comparison (it looks good to me but you never know).
Next would be, if this view is not directly attached to the window (or other top-level object if you're using xib's) you may have to also return true in any parent views. For the sake of testing you might just want to overwrite:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIIinterfaceOrientation)interfaceOrientation
{
return true;
}
for all view (controllers) in the tree.
this could be just a typo, but its not UIIinterfaceOrientation, its UIInterfaceOrientation
The method you want to override is shouldAutorotateToInterfaceOrientation. Your method signature is missing the Orientation on the end.
Make sure that the ViewController in which you implement this method, is the window's rootViewController.
Also, using return (interfaceOrientation != UIIinterfaceOrientationPortraitUpsideDown) is nicer to read. ;)
Does -shouldAutorotateToInterfaceOrientation get called at all? Try logging something in this method...
Your looking in the wrong place: in your RootViewController.m file look for the following code:
#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
//
//lots of useless comments
//
return (UIInterfaceOrientationIsPortrait(interfaceOrientation) ); //THIS LINE HERE
the line that says return (UIInterface... Portrait) is the line that determines your app's rotating capabilities. You can change this to whatever to allow you to be able to rotate completely, keep it at a certain orientation, or whatever you desire...
also in this
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIIinterfaceOrientation)interfaceOrientation
{
/* return (interfaceOrientation == UIIinterfaceOrientationPortrait || interfaceOrientation == UIIinterfaceOrientationLandscapeLeft || interfaceOrientation == UIIinterfaceOrientationLandscapeRight); */ //GET RID OF ALL THIS CRAP
return true; //do this instead, and if this doesn't work, try return YES;
}
I'm not sure it's a type error or edition difference, in my latest Xcode the method is like this:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
and it works well, are you missing the "to" of the argument?

Remove ability for portrait orientation for app in iPhone SDK

Is there a way where I can only allow my app viewable in landscape mode? I've managed to default the application's orientation to landscape, however in the iPad simulator, when I do Command->Arrow, the application rotates to portrait. I've removed the listings in the plist under "Supported interface orientations" for both of the Portrait entries, however that doesn't seem to've changed anything.
Any idea?
In your view controller, there's a delegate method for this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Do a project find for "autorotate", and edit the methods you'll find accordingly.
I believe you need tell the view that the specified orientation is not supported
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/shouldAutorotateToInterfaceOrientation:
Actually it is
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Things have changed with some updates. Currently, you'll simply need to add the following method:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
that will only allow landscape. Turn the phone all you like and it will only stay in either left or right landscape. Have fun :)