shouldAutorotateToInterfaceOrientation with iPad and iPhone differences - iphone

I have an iPhone app which I have created as a universal iPad/iPhone app. I've implemented a splitviewcontroller for the iPad version... all fine.
In my iPhone app, everything is in Portrait, except for a 2nd level view controller (a web view), which I override shouldAutorotateToInterfaceOrientation to allow landscape. On returning up the view chain I go back to portrait.. Excellent!
However, Now my iPad split view app is forced to stay in portrait. If I override shouldAutorotateToInterfaceOrientation in any of my views like rootviewcontroller or others it effectively allows landscape mode in my iPhone app which I cannot do. However it does fix my landscape problem in the iPad.
Is there a way round this? I effectively want to say YES to shouldAutorotateToInterfaceOrientation for iPad, but no for iPhone. I tried this, but it doesnt work, it allows landscape on both devices:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
BOOL rotate = NO;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
rotate = YES;
}
return rotate;
}
Any advice?

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
} else {
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

Related

Screen rotation on a per view basis

With iOS7, even though the app has the Device Orientation under General set to landscape and portrait. There are some screens in my app that I do NOT want to be able to rotate.
How can you pick and choose which views get rotated when the device turns?
I tried to use shouldAutorotateToInterfaceOrientation but I could not get it to work.
Anyone accomplished this with the new software?
try the following code in the views that you want it to be just landscape and change the landscape part to portrait in the views that you want it to be only portrait.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
Try overriding - (BOOL)shouldAutorotate and return NO.

ios6 Rotation issue from landscape to portrait mode

I am using New orientation methods of ios 6 and it is working fine. My view is presenting in portrait mode and when I presentviewcotrnoller and rotate it to landscape , dismiss that viewcontroller it reverts orientations. means it should remain in landscape but it becomes to portrait.
Here is my code.
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
// Tell the system It should autorotate
- (BOOL) shouldAutorotate {
return YES;
}
// Tell the system which initial orientation we want to have
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationMaskPortrait;
}
I think it is happening because of preferredInterfaceOrientationForPresentation method ,but not getting the solution for this.Please Help !
Thanks !!
If you want all the interfaceOrientations supported at the start, do not write the preferredInterfaceOrientationForPresentation method, as it would take just that preferred Interface orientation all the time.

Can I make one view in my app appear in Landscape and all others in portrait mode?

My iPhone app is all in portrait mode at the moment.
But I need to play a video that in widescreen mode and draw some ui elements that work best in portrait mode. Also there will be some text input in the screen at times, so keyboard will need to appear as portrait too.
Is it possible to tell a UIViewController when it loads that it needs to go to Landscape, the parent view on the stack will be Portrait.
I dont want to detect the user rotating, I want to decide which screens will be landscape and which will be portrait.
So to recap, everyscreen/view on my app will be portrait, except one that I would like to make Landscape. Can it be done?
Many Thanks,
-Code
In your UIViewController you implement this method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
BOOL result = NO;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
result = YES;
}
return result;
}
And you return YES for the orientation you want to support.
This method will be call on your UIViewController when it load, so it won't get call every time you enter in that UIViewController.
And if you want to be absolutely certain that all other UIViewController are the orientation you want, implement this method in them as well.
Yup, use [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]]; or portrait for portrait orientation.
override func viewWillAppear(animated: Bool) {
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
UIApplication.sharedApplication().statusBarHidden = true
}
override func shouldAutorotate() -> Bool {
return true
}
and change it to what you want (LandscapeRight , LandscapeLeft or Portrait)

Unable to lock screen orientation to portrait only in xcode 4?

In xcode 4 I am unable to lock the screen orientation to only portrait even though I have portrait selected only. How to I do this programmatically?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
this code works in a new blank project
The accepted answer did not work for me either. This did:
In the properties file for your app (YOURAPPNAME-Info.plist), located in the "supporting files" group, there is an array called "Supported interface orientations". Remove both landscape values from the array and your app will be locked in portrait orientation.
For each view controller (or just the parent), implement -shouldAutorotateToInterfaceOrientation: Set the default orientation to portrait and then do:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
Alternately, you can do:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
which will allow a rotation to portrait but not away from it.

Tabbar Nav app, allowing one view to rotate while others do not

I have a tab bar application in which i have 3 diffrent views each with there own view controller.
In the tab bar code i have this, to handle rotation.
#import "RotatingTabBarController.h"
#implementation RotatingTabBarController
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#end
Then in the 2nd view controller that i want to rotate depending on device orientation i have:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
And For the other two views that i do not want to rotate i have this method set.
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
The PROBLEM: so this works fine in view 1 and view 3 when u rotate the device they stay in portrait mode which is desired. When in view 2 i rotate to landscape, the view does as expected and rotates to landscape. BUT when click view 1 or view 3 tab while in lanscape mode in view 2, View 1 and View 3 are in landscape mode.
I can't figure out how to force them in portrait even if view 2 rotates to lanscape.
Any one know how to do this?
There's a big discussion[1] on this dating back from 2008 until now (look at comments down a few pages) -- summarily it seems like
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
or
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
or
[application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
will let you force it to landscape -- you would want to do this when the user goes back to your landscapey view(s) programmatically.
[1] iPhone app in landscape mode, 2008 systems