UInavigation controller did not work - iphone

I am making app like whenever the device change it's orientation mode it changes it's view.
At first when the view is portrait it shows perfect SearchViewController, then when i rotate to landscape it push to new view MapView in landscape ... now when i again change the view to portrait the map rotate to portrait... but it should be do to search view controller... and one more thing when i tapped detail disclosure button it should be go to back to search view controller... i think navigation controller not work in map view..
this is my code part of searchViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if(interfaceOrientation == UIInterfaceOrientationPortrait|| inte rfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
}
else {
MapView *mapView = [[MapView alloc] initWithNibName:#"MapView" bundle:nil];
mapView.title = #"Map";
[self.navigationController pushViewController:mapView animated:YES];
return YES;
}
return YES;
}
and this is my MapView code
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
// return (interfaceOrientation == UIInterfaceOrientationLandscapeRight|| UIInterfaceOrientationLandscapeLeft);
if(interfaceOrientation == UIInterfaceOrientationPortrait|| interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"hi Potrait");
[self.navigationController popViewControllerAnimated:NO];
return YES;
}
else {
}
return YES;
}

shouldAutorotateToInterfaceOrientation: is an incorrect place to implement any kind of navigation. You should do it in didRotateFromInterfaceOrientation: instead. Check the current interfaceOrientation and push or pop if necessary.
In searchViewController,
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if ( UIInterfaceOrientationIsLandscape(self.interfaceOrientation) ) {
MapView *mapView = [[MapView alloc] initWithNibName:#"MapView" bundle:nil];
mapView.title = #"Map";
[self.navigationController pushViewController:mapView animated:YES];
}
}
In MapView,
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if ( UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ) {
[self.navigationController popViewControllerAnimated:NO];
}
}
And alter your shouldAutorotateToInterfaceOrientation: to
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

Related

Force portrait in one view controller makes other to be in portrait initially

The root view controller of navigation controller supports only portrait orientation and other controllers supports all orientation.Now if i am on the root view controller and the DEVICES is in landscape and if i push next view controller that opens in portrait that should open in landscape as it supports all orientation.
Please help me with this.
Using iPhone 4s iOS6.1.3
you can check Device orientation in your first screen after login viewcontroller using bellow code:-
-(void)viewWillAppear:(BOOL)animated
{
[self willRotateToOrientation:[[UIDevice currentDevice] orientation]];
[super viewWillAppear:YES];
}
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {
//set your landscap View Frame
[self supportedInterfaceOrientations];
}
}
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
//set your Potrait View Frame
[self supportedInterfaceOrientations];
}
}
// Handle rotation
}
sor when you load this viewcontroller it check first device oriantation and then load it's related frame
I think this is the issue related to the orientation changes in iOS6. You need to subclass the UINavigationController
Check this
1 . You have to create sub class of UINavigationController. add Following method.. Take one boolean variable to check whether it support for all orientation or not and change its value.
#implementation NavigationControllerViewController
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
AppDelegate *appdelgate=[[UIApplication sharedApplication]delegate];
if (appdelgate.issuppoertAll) {
// for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
#end
2 when you navigate form root view controller to other view controller
use this code , when you want to forcefully change its orientation.i.e lanscape to portrait
obj_viewcontroller = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:obj_viewcontroller animated:NO];
[self dismissModalViewControllerAnimated:NO];
[self.navigationController pushViewController:obj_viewcontroller animated:NO];
3 In second view controller you have to change boolean variable value
-(void)viewWillAppear:(BOOL)animated
{
appdelgate.issuppoertAll=YES;
}
4 Add this method into all view controller and set orientation as per your need.
- (NSInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

Back Transition Wrong in UINavigationController + UITableViewController in Landscape Orientation

I'm calling up a UINavigationController and usually allowing it to use any major orientation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
// return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
BOOL isAllowed;
if ([allowedOrientations isEqualToString:#"portrait"]) {
isAllowed = (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
} else if ([allowedOrientations isEqualToString:#"landscape"]) {
isAllowed = (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
} else {
isAllowed = YES;
}
return isAllowed;
}
From there I call a UITableViewController:
myTable = [[SimpleDocViewerTable alloc] initWithFrame:thisFrame];
self = [super initWithRootViewController:myTable];
Where:
#interface SimpleDocViewerTable : UITableViewController {
The table view controller and navigation controller mostly seem integrated correctly. When I push a new page onto the navigation controller, it correctly scrolls right to left, no matter which orientation I'm using:
SimpleDocPageVC *thisVC = [[SimpleDocPageVC alloc] initWithView:docView];
thisVC.title = [[[tableEntries objectAtIndex:indexPath.section]
objectAtIndex:indexPath.row] objectForKey:#"title"];
[self.navigationController pushViewController:thisVC animated:YES];
However, when I exit that pushed page, if the iPhone is in landscape mode, it scrolls bottom to top, rather than right to left.
The popViewController is all taken care of magically within the automatic "back" button that's generated, so it should be doing the right thing ... but doesn't.
Could it be that one of your controllers (SimpleDocViewerTable, or SimpleDocPageVC) does not support both orientation in shouldAutorotateToInterfaceOrientation?

UIViewController gets stuck in horizontal orientation after portrait rotation

View Controller A displays View Controller B in horizontal orientation
#pragma mark Rotation Delegate Methods
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
[landscapeChartViewController.chartImageView reloadWithUrl:
[NSString stringWithFormat:#"someurl",[symbol uppercaseString]]];
NSLog(#"showing chart");
[self presentModalViewController:landscapeChartViewController animated:NO];
}
}
This works fine. View Controller B shows up in landscape orientation. Here is View Controller B's implementation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
NSLog(#"dismissing chart");
[self.parentViewController dismissModalViewControllerAnimated:NO];
}
}
The problem is, when I go back into portrait orientation to show View Controller A, View Controller A is stuck in landscape orientation. How can I fix this?
EDIT: after reading your comment, I suggest trying to use willRotateToInterfaceOrientation:duration: instead of willAnimateRotationToInterfaceOrientation, like this:
controller A:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
[landscapeChartViewController.chartImageView reloadWithUrl:
[NSString stringWithFormat:#"someurl",[symbol uppercaseString]]];
NSLog(#"showing chart");
[self presentModalViewController:landscapeChartViewController animated:NO];
}
}
controller B:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
NSLog(#"dismissing chart");
[self.parentViewController dismissModalViewControllerAnimated:NO];
}
}
I do more or less the same in a project of mine, only between two non modal views.
One option is to move your code from willAnimateRotationToInterfaceOrientation: to didRotateFromInterfaceOrientation: and use self.interfaceOrientation in place of toInterfaceOrientation.
View Controller B shows up in landscape orientation. Here is View Controller B's implementation:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
NSLog(#"dismissing chart");
[self dismissModalViewControllerAnimated:NO];
}
}
Did you implement the function willRotateToInterfaceOrientation? Also try using the Notification centre to notify the parent view controller that your modal view controller is rotated and then simply [self dismissModalViewControllerAnimated:YES]
I always write my orientation logic in didRotateFromInterfaceOrientation.
Here is a part of my code it works fine....
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[connectCoverLockUnlockSwitch setFrame:CGRectMake(250,6,51,31)];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
[connectCoverLockUnlockSwitch setFrame:CGRectMake(400,6,51,31)];
[self.tableView reloadData];
}
else if (orientation == UIDeviceOrientationPortraitUpsideDown || orientation == UIDeviceOrientationPortrait){
[connectCoverLockUnlockSwitch setFrame:CGRectMake(250,6,51,31)];
}
}
else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationUnknown || orientation == UIDeviceOrientationFaceUp) {
//return;
}
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
[connectCoverLockUnlockSwitch setFrame:CGRectMake(570,6,51,31)];
[self.tableView reloadData];
}
else if (orientation == UIDeviceOrientationPortraitUpsideDown || orientation == UIDeviceOrientationPortrait)
{
[connectCoverLockUnlockSwitch setFrame:CGRectMake(330,6,51,31)];
[self.tableView reloadData];
}
}

Load mainView with shouldAutorotateToInterfaceOrientation

I am trying to load a view with the shouldAutorotateToInterfaceOrientation method, but I have a problem. When the device runs as landscape, I want to show the main view itself, and when when the device is portrait, load a custom UIView.
Here is my code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft)|| (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
//doesn't show main view !!
self.view;
else if ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
//works fine !
self.view = portraitView;
return YES;
}
I figured out !!!! just call the view with modal on a method
- (void) mainView {
screenTestViewController *mv = [[screenTestViewController alloc]initWithNibName:#"screenTestViewController" bundle:nil];
[self presentModalViewController:mv animated:NO];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft)|| (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
[self mainView];
.
.
.
You should push your custom UIView in: willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

pushViewController to open a view in landscape

I am trying to open a view in landscape by pushing on navigation controller but it always open in portrait.
First view is in portrait and when I click on a button then next view should be in landscape.
I am trying following code
Calling View:
ResultViewController *resultView = [[ResultViewController alloc] init];
[[self navigationController] pushViewController:resultView animated:YES];
View that should open in landscape:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Is there something else to try here?
Thanks
Try..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//return UIInterfaceOrientationIsLandscape(interfaceOrientation);
return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}
and then...
- (void)viewDidLoad {
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:YES];
}
in resultView