How to stop rotation of ABPersonViewController & ABNewPersonViewController in Landscape mode in iphone - iphone

I am using a ABPersonViewController and ABNewPersonViewController class by pushview controller.
ABPersonViewController *pvc = [[ABPersonViewController alloc] init];
[pvc setPersonViewDelegate:self];
[[self navigationController] pushViewController:pvc animated:YES];
In ABPersonViewController and ABNewPersonViewController page it is displaying in portrait mode. But when I rotate my iPhone then it is perfectly rotating in landscape mode. But I want to stop this rotation. If I rotate my iPhone in landscape mode it's view should be in portrait mode.

The solution for your problem is quite simple: just subclass UINavigationController like this:
#interface UINonRotatingNavigationController : UINavigationController {
}
#end
in your .h file, and in your .m file type:
#implementation UINonRotatingNavigationController {
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Use it as your main navigation controller for the person picker - this should block it from rotating.
Hope this was helpful,
Paul

Related

How to load a UIViewController in landscape mode if its parent is in Portrait mode?

I want to load a ViewController in landscape mode from a portrait parent. Parent is got fixed for portrait, it wont change its orientation, however the child view controller whichever is got pushed from the parent also loading portrait initially irrespective of the device orientation (if we rotate the device few more times then the orientation set properly for childs). So how to load child properly in initial time itself.
or
Is there any way I can set the orientation programatically, so that I can use it in ViewWillAppear method.
Thanx.
In your child UIViewController set these two methods:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
In parent UIViewController do:
[self.navigationController presentViewController:controller animated:YES completion:nil];
instead of:
[self.navigationController pushViewController:controller animated:YES];
If you want to present navigation bar in child:
DetailViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
controller.title = #"Child";
You'll have to subclass UINavigationController with the two methods mentioned above instead in the child view controller.
MyNavigationController *nav = [[MyNavigationController alloc] initWithRootViewController:controller];
[self.navigationController presentViewController:nav animated:YES completion:nil];
Try this sample Project...........
https://www.dropbox.com/s/lrsz4dpeolpeu23/RotationDmeo.zip
If i got it right, you just add this methods to your child view controller
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft / Right;
}
To present your view use
[self presentViewController:viewController...]
And your view is presented in Landscape mode.
Use this in view did appear or will appear,
[[UIDevice currentDevice]performSelector:#selector(setOrientation:) withObject:(__bridge id)((void *)UIInterfaceOrientationLandscapeRight)];
Before that Enable all 4 orientations in your .plist
Add this on AppDelegate.m
- (NSUInteger)application:(UIApplication*)application
supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
UIViewController *cont=self.vc;
if([cont isKindOfClass:[YourClass class]])
{
NSUInteger orientations = UIInterfaceOrientationMaskLandscapeRight;
NSLog(#"Landscape");
return orientations;
}
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
return orientations;
}

iOS - Change storyboards upon rotation - Problems with Portrait

Ok so I have been testing out my own methods for changing the presented storyboards upon rotation and have been doing so with the -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation method to be assigning self to another method which runs an if-else statement which checks the orientation and loads the correct storyboard appropriately. The problem I'm having is the checking if it's in portrait mode or the else part of the statement. When I run the app in simulator and rotate the device to landscape, it loads the landscape storyboard like it should. But when I rotate the device back to portrait mode, it doesn't change back to the portrait storyboard. At first I thought the problem was that I wasn't dismissing the view controllers but after doing that, it still didn't work. I realized that it was the else part of the statement that wasn't working. I stuck anNSLog(#"PMComplete") and still after running the app again and rotating to landscape and back, it still didn't show up in the log. Does anyone have any thoughts on what's wrong here? I am operating this on the viewcontroller for the portrait storyboard (assigned the view controller in the portrait storyboard the ViewController class). I also created this app without storyboards and created them from scratch and assigned the portrait one as the one that loads up from the AppDelegate.
Here is my ViewController.m file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize isLandscape;
#synthesize isPortrait;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewChanged {
UIInterfaceOrientation theOrientation = self.interfaceOrientation;
UIStoryboard *portraitStoryboard;
UIStoryboard *landscapeStoryboard;
portraitStoryboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
landscapeStoryboard = [UIStoryboard storyboardWithName:#"LandscapeStoryboard" bundle:nil];
UIViewController *portraitViewController = [portraitStoryboard instantiateInitialViewController];
UIViewController *landscapeViewController = [landscapeStoryboard instantiateInitialViewController];
if (theOrientation == UIInterfaceOrientationLandscapeLeft ||
theOrientation == UIInterfaceOrientationLandscapeRight) {
[portraitViewController dismissViewControllerAnimated:NO completion:nil];
[self presentViewController:landscapeViewController animated:NO completion:nil];
NSLog(#"LSComplete");
}else{
[landscapeViewController dismissViewControllerAnimated:NO completion:nil];
[self presentViewController:portraitViewController animated:NO completion:nil];
NSLog(#"PMComplete");
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self viewChanged];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Allow Rotation for QLPreviewController independent of parent View

my app doesn't support rotation. But I want to present a QLPreviewController that supports rotation.
I present the QLPreviewController like this:
[self presentModalViewController:thePrevController animated:NO];
How can I do this?
Enable all rotations in your application plist file. This will make all views rotate irrespective of the settings in the view controller.
Then subclass your root UINavigationController as below, adding the rotation control code for iOS5 and 6 depending on your requirements:
I was updating an old app with MainWindow.xib, so I changed the class of the navigation controller in the xib file to CustomNavigationController. But in a more modern app with say a main menu, you'd instantiate the nav controller like this:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MainMenuVC *masterViewController = [[MainMenuVC alloc] initWithNibName:#"MainMenuVC" bundle:nil];
self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
Subclass UINavigationController
#import <UIKit/UIKit.h>
#interface CustomNavigationController : UINavigationController
#end
#import "CustomNavigationController.h"
#interface CustomNavigationController ()
#end
#implementation CustomNavigationController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
#end
Then subclass the QLPreview controller so you can override the rotation code which will enable rotation for the QLPreviewController only. The rest of the app with views pushed from your CustomNavContoller will not rotate as the CustomNavigationController is locked.
I added this interface and implementation at the top of the view controller where I wanted to present the QLPreviewController.
#interface RotatingQLPreviewController : QLPreviewController
#end
#implementation RotatingQLPreviewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
return YES;
}
#end
Then present your QLPreviewController using your subclass.
RotatingQLPreviewController *preview = [[RotatingQLPreviewController alloc] init];
preview.dataSource = self;
[self presentViewController:preview
animated:YES
completion:^(){
// do more stuff here
}];
This method should work for other modal views that you want to rotate, but I haven't tried it.
I implemented this method in the latest app I'm working on and works in both iOS5 and 6.
Hope it helps.

How to hold NavigationController in subViewController orientation?

I have made a NavigationController structure.
FirstViewController is RootViewController, SecondViewController is a next SubViewController. Each ViewController shouldAutorotateToInterfaceOrientation is a different(refer a following code).
FirstViewController is only portrait available. SecondViewController all support Portrait and landscapeMode.
In FirstViewController after sth button touch, call a pushHandler. next SecondViewController is a pushed.
In SecondViewController after rotated landscape back to the FirstViewController, that orientation is too landscape.
but, I implemented each ViewController orientation different. however each ViewController not independently set orientation. why happen?
How can I viewcontroller each orientation can be set independently, to do?
If SecondViewController change the orientation of the landscapeMode. I want back to the FirstViewController still portraitMode(forever hold portraitState Anything not to be affected).
How do I implements programmatically?
FirstViewController *rootViewController = [FirstViewController alloc] init];
UINavigationController *MyNavigationController = [UINavigationController alloc] initWithRootViewController:rootViewController]];
//FirstViewController
- (void)pushHandler
{
SecondViewController *subViewController = [SecondViewController alloc] init];
[[self navigationController] pushViewController:subViewController animated:YES];
[subViewController release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//SecondViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
Because you use a UINavigationController for all your UIViewControllers whatever orientation behaviour you will set for the UINavigationController will be applied to all it's children or in otherwords objects on its stack.
Think of a UINavigationController as a house and UIViewControllers as rooms. All the rooms in the house will rotate in the same way the house. There is no way to turn a room without turning the house and other rooms.
But as always there are tricks. Look at my answer to this question Only having one view autorotate in xcode?

Orientations support

I have an app with some views, I need one view to rotate. that view is called by pushviewcontroller. all these views are inside a tabbarcontroller.
I already edit the info.plist to support orientations I added this items to Supported interface orientations:
Landscape (left home button)
Landscape (right home button)
and also added this to the view i want to rotate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
but it doesnt work.
what do i need to do? Thanks
UITabBarController have a some problem. the problems is a subviews(selected index) not autorotate.
So, you can make a category, and add a below code.
and, add a #import "UITabBarController+Autorotate.h"
#import <Foundation/Foundation.h>
#interface UITabBarController (Autorotate)
#end
#import "UITabBarController+Autorotate.h"
#implementation UITabBarController (Autorotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]])
controller = [(UINavigationController *)controller visibleViewController];
return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#end