I have more than ten ViewControllers in portrait mode, but I need to force a single one in Landscape mode regardless of the deviceĀ“s orientation.
Here is the solution:
1) Embed the LandscapeViewController in a subclassed NavigationController and connect it from your PortraitViewController using a modal segue.
2) Subclass UINavigationController
LandscapeNavigationController.m
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
3) Don't forget to dismiss your modal VC (in this case from my Bar Buttom Item Action)
- (IBAction)goBack:(id)sender
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
Related
Hi my TabBarViewController Hierarchy is like this. All ViewControllers attached with tabbar are without navigationController.
UIViewController (Home View), when Pushed it navigate to tabBar based ViewController at index 0, User Can come back from tabBarViewControllers to home viewController at any time using back button in navigation bar.
UITabBarViewController (BaseViewController)
-ViewController0,(NO Navigation ViewController)
-ViewController1 (NO Navigation ViewController)
-ViewController2 (NO Navigation ViewController)
-ViewController3 (NO Navigation ViewController)
-ViewController4 (NO Navigation ViewController)
I used this Approach of Tabbar based ViewController, because Tabbar is not Home ViewController.
I want to auto rotate only ViewController2 in Portrait and Landscape. My Project is only in Portrait Mode.
I tried many thing like THIS, But it not getting.
Hi After lot research What i have found, whether it is Tabbar or UIVicontroller.
As per my Question, My project is in Portrait Mode and I want only single view Controller Auto rotation.Below are the steps, which helped me.
1 - In App Delegate.h
#property (assign, nonatomic) BOOL shouldRotate;
2 - In App Delegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED
{
_shouldRotate = [[NSUserDefaults standardUserDefaults]boolForKey:#"rotateKey"];
NSLog(#"Did I get to InterfaceOrientation \n And the Bool is %d",_shouldRotate);
if (self.shouldRotate == YES){
return UIInterfaceOrientationMaskAll;
}else{
return UIInterfaceOrientationMaskPortrait;
}
}
3 - Now Which UIViewController, You want Auto rotation,
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
BOOL rotate = YES;
[[NSUserDefaults standardUserDefaults]setBool:rotate forKey:#"rotateKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
-(BOOL)shouldAutorotate
{
return YES;
}
4 -Trick Part is
If you do all above steps, your view controller will get auto rotated, if u come back from current view controller, which is landscape mode. the previous view controller will get auto rotate in landscape and it will keep chain.
So, to avoid this,
If you are going from view Controller A to View Controller B. View Controller is auto- rotation, then in View Controller A-
5 - Use this code -
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
BOOL rotate = NO;
[[NSUserDefaults standardUserDefaults]setBool:rotate forKey:#"rotateKey"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
-(BOOL)shouldAutorotate
{
return NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
dispatch_async(dispatch_get_main_queue(), ^{
// UI Updates
});
return UIInterfaceOrientationMaskPortrait;
}
In my application I am trying to force a view controller to be portrait only. I am using the code below but nothing is really happening. I am still able to see the screen in landscape as in portrait?
I have enabled landscape (left/right), portrait in my project settings.
What am I do wrong?
ViewController.m
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
If you VC is embedded in a navigation or tab bar controller you need to create a category on that controller. This is an example that allow navigation controller rotate in all ways:
#import "UINavigationController+RotationAll.h"
#implementation UINavigationController (RotationAll)
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
#end
I am developing ios application that uses two viewcontrollers A and B. A supports both orientations landscape and portrait, when B is only in portrait mode. my goal is B viewcontroller always to be only in portrait mode when I navigate from A to B .I make restrictions on B viewcontroller in the navigationcontroller.
- (BOOL)shouldAutorotate
{
_navigation = (MBNavigationController *) self.frontViewController;
id currentViewController = [_navigation visibleViewController];
if ([currentViewController isKindOfClass:[MBViewController class]])
return NO;
return YES;
}
when A is in portrait orientation and I navigate to B everything works fine. but when A is in landscape mode and I do the same, part of the B controller view is out scenes. please help me to find the right solution.
One possible solution is present the B viewController with another navigationController in which
// sysversion>=ios6
- (BOOL)shouldAutorotate
{
return NO;
}
// sysversion<=ios5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
My iPad app has a UIViewController called MainViewController which presents a modal page sheet UIViewController called ModalViewController. ModalViewController then presents a UIImagePickerController.
Here is MainViewController.m:
#implementation MainViewController {
...
- (BOOL)shouldAutorotate
{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
...
}
So it rotates to either of the landscape orientations, and when ModalViewController is presented it rotates that correctly also.
However when the UIImagePickerController is presented, it allows rotation to landscape or portrait orientations, and this allows MainViewController and ModalViewController to rotate to un-desired orientations.
How can I prevent MainViewController and ModalViewController from rotating when the UIImagePickerController is on the screen?
Is this the same problem asked here Stop a modal UIViewController from rotating ? If you're using a UINavigationController in the end you can subclass it and control the mechanism for rotation
So I am developing an iPad app that supports only landscape mode except for on one modal view controller. The issue I am having is that once I present the modal view and change the orientation to portrait then dismiss the view, the parent view (which should only support landscape) is in portrait mode until I rotate the device in which it then goes back to landscape and stays that way. I have been beating myself up trying to figure out how to keep the parents view original orientation but haven't been able to find a solution.
I have the following code in my app delegate to allow orientation changes on only that single modal view (GalleryPhotoViewer) :
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
//Support Portrait mode only on Photoviewer
if ([[presentedViewController presentedViewController] isKindOfClass:GalleryPhotoViewController.class] ) {
orientations = UIInterfaceOrientationMaskAll;
}else{
orientations = [presentedViewController supportedInterfaceOrientations];
}
}
return orientations;
}
From the parent class (PhotosViewController) I am calling :
GalleryPhotoViewController *gpView = [GalleryPhotoViewController new];
[self presentViewController:gpView animated:YES completion:nil];
Also in my parent (and other views) I have the following code to disallow portrait mode :
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
} else {
return NO;
}
}
Any ideas on how I can keep the orientation on my parent view? I was thinking about possibly just programmatically changing the orientation on the parent in the viewWillAppear method once the modal is dismissed but then I wouldn't know what the previous orientation was, not to mention I haven't been able to find code to do this regardless for ios6.
EDIT/SOLUTION : So I found a solution and what I ended up doing was leaving the application:supportedInterfaceOrientationsForWindow: code and just adding the UINavigation subclass to the parent view that was presenting the modal view and everything worked as expected and the parent retained its original orientation while the modal was able to change freely.
In my parent :
//To make sure that this view remains in Landscape
#implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
#end
Thanks #matt for the suggestions.
I think the problem is your use of application:supportedInterfaceOrientationsForWindow:. Instead, get rid of that, and start with a UINavigationController subclass and make that the class of the root view controller that is your navigation interface. Then:
In the UINavigationController subclass, return UIInterfaceOrientationMaskLandscape from supportedInterfaceOrientations.
In the presented (modal) view controller, return UIInterfaceOrientationMaskAll from supportedInterfaceOrientations.