Load mainView with shouldAutorotateToInterfaceOrientation - iphone

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

Related

iPhone interface orientation change not returning to portrait orientation

So I have a view that I present modally when the interface orientation changes to landscape. However when the orientation returns to portrait and the modal view dismisses itself, the tableview from the initial view remains in landscape orientation (this table view must be only in portrait orientation)
Here is the code :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight) );
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
[self performSegueWithIdentifier:#"showCatChart" sender:self];
}
if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
[self refreshTableView];
}
}
I tried to refresh the tableview but that doesn't make it portrait again ...
this could be from the view hierachy ...
NavigationController->tableView (only portrait)->tableview->landscapeViewModalController
With iOS 6, you need to implement the following:
- (BOOL)shouldAutoRotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
In your AppDelegate, you need to change the following:
[self.window addSubview:viewController.view];
to
[self.window setRootViewController:viewController];
Also, keep your current code if you want to support previous versions of iOS.
Use the following in appdelegate.
[self.window addsubview:viewcontroller];
This alone will solve your orientation problem.

present modal view on rotate

So I have a UITableViewControler displaying a tableview in portrait mode.
As soon as i rotate the iPhone i want to present a modal view in landscape mode.
In the tableView i use:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
And to handle the present the modal view:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if((interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft))
{
NSLog(#"Push page view");
PagingViewController *s = [[PagingViewController alloc] initWithNibName:#"PagingView" bundle:nil];
s.hidesBottomBarWhenPushed = YES;
[self presentModalViewController:s animated:YES];
[s release];
}
}
The modal view i have the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
And to dismiss the modal view it self, I do:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
NSLog(#"Dismiss my self");
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
}
Some how this works two times.
The third time i rotate the iPhone from Portrait mode to Landscape mode, i get a bad access error.
I cant figure out what gives me the error.
Anyone care for a shot?
The simplest way I can think of is to implement -shouldAutorotate... and dismiss the modal view and return NO to abort rotation. Perhaps that will be sufficient to avoid any concurrency issues. If this suggestion isn't to your liking take a look at NSNotificationCenter.

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

How to enforce landscape orientation for child view, but not parent view?

I have a UINavigationController (Parent) that is pushing a UIViewController (Child). I understand that both need to support:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES; //(interfaceOrientation == UIInterfaceOrientationPortrait);
}
However, I don't want the parent to be able to rotate to landscape orientation. How can I enforce this?
UPDATE:
My Parent has been updated to:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation != UIInterfaceOrientationLandscapeRight ||interfaceOrientation != UIInterfaceOrientationLandscapeLeft )
return NO;
else
return YES;
}
But now the child doesn't rotate.
In your parent View Controller you will need to implement this. If you have not already subclassed the UINAvigationController you are using for the parent, just do that and add this method.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation != UIInterfaceOrientationLandscape)
return NO;
else
return YES;
}
In the child View COntroller subclass, implement the method like you did:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES; //(interfaceOrientation == UIInterfaceOrientationPortrait);
}

iPad Rotation, Any Way To Load Custom View Based On Orientation?

Im writing an app that I would like to display different views based on orientation. For example, if the device is portrait load pView, if landscape load lView. Below is the code ive currently tried.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
self.view = landscape;
}
}
With this I have created 2 views in IB and connected the outlets to the right view. Ive also tried this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
self.view = portrait;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
lView *abo = [[lView alloc] initWithNibName:#"lView" bundle:nil];
[self.navigationController pushViewController:abo animated:NO];
[abo release];
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
lView *abo = [[lView alloc] initWithNibName:#"lView" bundle:nil];
[self.navigationController pushViewController:abo animated:NO];
[abo release];
}
}
The code directly above worked for ipod but not ipad. Any ideas?
I found the following 2 (Apple) sample projects useful:
http://developer.apple.com/library/ios/#samplecode/AlternateViews/Introduction/Intro.html
http://developer.apple.com/library/ios/#samplecode/WhichWayIsUp/Introduction/Intro.html
Also, remember from a device perspective there is also face-up and face-down orientations; this caught me out.
I have to ask the obvious question:
Do you have the screen orientation lock switch turned on for your iPad?
Depending on what you are trying to do, you can set up your interface in IB to handle rotations automatically. You can resize components, move pics and labels around, stuff like that. That may do what you want to do...