I have a universal iOS (7.1+) app that I would like to only support portrait orientation for most views. I would like 1 view to autorotate.
The application is a navigation based application (i.e. the base view is a UINavigationViewController). In the project editor under Deployment Info I have checked portrait, landscape left and landscape right as the supported orientations.
I then restricted the autorotation of the app by implementing the -(BOOL)shouldAutorotate method and the - (NSUInteger)supportedInterfaceOrientations in my base navigation controller. As I understand it, this will restrict the orientation in any view displayed by the navigation controller.
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
The view I would like to autorotate I present modally. In the view controller that I present modally I also set -(BOOL)shouldAutorotate and - (NSUInteger)supportedInterfaceOrientations.
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
}
This solution works fine on iPhone! The one modal view rotates as expected and the other views inside the However, the same view using the same code does not rotate on the iPad. I am confused as to why it works on the iPhone and not on the iPad. It seems to me that since it is the exact same view controller that it should rotate on both.
Related
I have five view controllers and In Project Target Device Orientation I have enabled Portrait ,Landscape Left and Landscape Right. Now I want 4 view controllers out of 5 view controllers Stay in Portrait mode (Not Rotate into landscape left and landscape right) and only one view controller rotate in all modes(Portrait,landscape left,landscape right). So How can do this please tell .
Implement -(NSUInteger)supportedInterfaceOrientations for each of your ViewControllers and specify which interface orientations each controller should support.
Edit
Assuming that you have a separate implementation for each of your ViewControllers, implement -(NSUInteger)supportedInterfaceOrientations and -(BOOL)shouldAutorotate in each of your implementations.
For example
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
will ensure that your view controller supports all landscape modes. Combine this with
-(BOOL)shouldAutorotate{
return YES;
}
and your display will "flip" over when rotated.
Use the enum UIInterfaceOrientationMask to adjust which orientations are supported and try different combinations of this along with a YES/NO return value to -(BOOL)shouldAutorotate until you get the behaviour you want.
First of all, in AppDelegate, write this.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
Then, For UIViewControllers, in which you need only PORTRAIT mode, write these functions
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
For UIViewControllers, which require LANDSCAPE too, change masking to All.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
//OR return UIInterfaceOrientationMaskAll;
}
Now, if you want to do some changes when Orientation changes, then use this function.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}
NOTE Please :-
A lot depends on with which controller is your UIViewController embedded in.
Eg, If its inside UINavigationController, then you might need to subclass that UINavigationController to override orientation methods like this.
subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.) did set it as self.window.rootViewController.
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
From iOS 6, it is given that UINavigationController won't ask its UIVIewControllers for orientation support. Hence we would need to subclass it.
I hold device in landscape mode and moved into second view controller, which only support portrait mode. when return back from second view controller(Support all orientation), First view controller is not rotating to landscape mode automatically.
if i did below code then its work in IOS6. but not working for IOS7.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
[UIViewController attemptRotationToDeviceOrientation];
}
in IOS7 viewController is rotating but status bar is not rotating
Implement the following in VC A:
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
The solution presented by AMayes is not quite correct.
The UIViewController docs state the following:
Typically, the system calls this method only on the root view
controller of the window or a view controller presented to fill the
entire screen; child view controllers use the portion of the window
provided for them by their parent view controller and no longer
participate directly in decisions about what rotations are supported.
Therefore, put the following code in the UINavigationController subclass:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
I have an app where landscape left and landscape right are both enabled in the build settings, but I'd only like these to be available in one ViewController in the app.
I'm using a navigation controller, and in the first ViewController I push onto the stack, I'd like to disable rotation altogether. I've tried all 3 of these with no success:
- (BOOL)shouldAutoRotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
When I rotate the device in the simulator, the layout still changes. I know the last method is deprecated in iOS 6.
Any ideas?
Fixed it. The reason was because the navigation controller was being rotated, and this was not triggering - (BOOL)shouldAutorotate to be called in the top view controller, as I thought it would.
I subclassed the navigation controller and added
- (BOOL)shouldAutorotate {
return NO;
}
I am using IASK in my app, and I just found a problem. That is when I'm going to change my device orientation, and the main viewcontroller is working well (both iOS 5 & 6). But in the IASK view controller it doesn't work! In addition, the viewcontroller always splash flips the view when I press the settings button to get into the IASK view controller. I am trying to figure out what's wrong in my code. I did add both view orientation methods for iOS 5 & 6 into IASK view controller as well, but it still doesn't perform correctly.
Does anyone know how to solve this kind of problem?
//ios5 autorotate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation==UIInterfaceOrientationPortrait)||(interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)) {
return YES;
}
else return NO;
}
//ios6 autorotate
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (BOOL)shouldAutorotate
{
return YES;
}
Frustrating. I've tried to make an app Portrait only.
In Targets => Summary => Supported Interface Options, only portrait is selected.
I've put the following on the navigation controller of my app:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate{
return NO;
}
And even copied the same onto the root view controller of the navigation controller. It's still being presented that the view is allowed to be rotated. I'm still able to rotate my app to landscape on the iPad, and possibly even upside down on the iPhone.
Thanks!