Problem pushViewController from Landscape to Portrait - iphone

I am trying to go from a viewcontroller that supports landscape (while in landscape mode), to one that explicitly doesn't (and shouldn't) support landscape. I'm doing this as follows:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
Ideally I want the new viewController that i'm pushing onto the stack to start off initially in portrait, not landscape. Strangely even with this method implemented, it starts off in Landscape.
My only guess is that Apple doesn't want a user transitioning from landscape to portrait (despite allowing us to go from landscape, back to a previous controller that is in portrait).
Any insights and/or help would be much appreciated.

I found a way to force portrait. It's a bit a hack, but here it is. In the -(void)viewDidLoad of the viewController that you want to force portrait for do the following:
UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
[viewController release];
This basically forces portrait, by presenting a controller (which only supports portrait by default).

You will need to present your new view controller modally. If your view controller exists within a navigation controller the orientation of all view controllers in the nav stack is implied by the root view controller in the stack. Whatever your root view controller in the nav stack returns from shouldAutoRotateToInterfaceOrientation is then used for all view controllers below it.

The answer by Sahil above is deprecated since iOS 6.0. However, the following seems to do the same trick:
UIViewController *viewController = [[UIViewController alloc] init];
[self presentViewController:viewController animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
[viewController release];

Related

Force Landscape iOS 6

I am looking to have one view in my app have landscape orientation. I have managed to get the view to stay in landscape when rotated manually, but if the device is already portrait, it stays portrait, regardless of its supported orientation (set using supportedInterfaceOrientations method) . Is there a way to get the view to rotate automatically? I have tried:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
but this doesn't work.
Any suggestions would be greatly appreciated!
One way to do this is by overriding preferredInterfaceOrientationForPresentation but in order for that to be called the viewController has to be presented (as in modal) and not pushed as mentioned here:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
NSLog(#" preferred called");
return UIInterfaceOrientationPortrait;
}
In order to present your viewController in a UINavigationController use:
UINavigationController *presentedNavController = [[UINavigationController alloc] initWithRootViewController:protraitViewController];
[self presentViewController:presentedNavController animated:YES completion:nil];
To make UINavigationController respect your current viewController's orientation preferences use this simple category instead of sub-classing.
Also, this part of Apple's documentation is a good read for understanding iOS orientation handling better.
Define the following in the UIViewController for your landscape-only view:
- (UIInterfaceOrientation) supportedInterfaceOrientations
{ return UIInterfaceOrientationLandscapeLeft; } // or right or both
This should prevent your view from ever appearing in portrait. From iOS documentation:
Declaring a Preferred Presentation Orientation
When a view controller
is presented full-screen to show its content, sometimes the content
appears best when viewed in a particular orientation in mind. If the
content can only be displayed in that orientation, then you simply
return that as the only orientation from your
supportedInterfaceOrientations method.

UIImagePicker can't perform full-screen transition on iOS 3.1.3

I am modifying my iphone app to make it backward compatible with iOS 3.1.3. I allow the user to load images from the photo library. I present the image picker with the following code:
UIImagePickerController* content = [[UIImagePickerController alloc] init];
content.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
content.delegate = self;
[self presentModalViewController:content animated:YES];
[content release];
This works correctly on ios 4.0+. However, on ios 3.1.3, the image picker never appears, and I get the following warning:
Can't perform full-screen transition. The fromViewController's view must be
within a view that occupies the full screen.
The fromViewController in this case is the visible view controller within a navigation controller. The navigation controller is set up in the appDelegate didFinishLaunchingWithOptions using the following code segment:
MyViewController* root = [[MyViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:root];
aNavigationController.delegate = self;
[window addSubview:aNavigationController.view];
Prior to attempting to load the image picker, another view controller is presented in the navigation controller. Therefore, at the time the image picker is loaded, two view controllers are in the navigation stack.
Based on another post, I have tried using the root view controller and the navigation controller as the fromViewController (the controller presenting the image picker). The behavior is the same.
I'm wondering if the problem has anything to do with the fact that the navigation controller's modalPresentationStyle cannot be set in iOS 3.1.3. For iOS 3.2+, I set the presentation style to UIModalPresentationFullScreen. I believe this is the default for previous iOS's. However, I'm suspicious simply because the warning I'm getting concerns full-screen views.
Can anyone provide any other suggestions? I have not been able to find any Apple documentation that addresses changes to UIImagePicker or UINavigationController from ios 3.x to 4.0.
Thanks in advance!
What I usually do when the view controller is inside a navigation controller is:
imagePicker = [[UIImagePickerController alloc]init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
[self.navigationController presentModalViewController:imagePicker animated:YES];
Hope it works for you!

IPhonesdk Orientation Issue

I have two views, on first view i have multiple buttons, i wanted that view to be landscape right so i put this code :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
it works fine as my application starts in landscape right mode. the buttons on this view pushes user to second view which i wanted to be a portrait mode. so i put that code in second view but its not working as i wanted.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
its showing my the orientation which i had on previous view. should i have to put something on viewdidload to make it works? please help. thanks
It's a bit a hack, but here it is. In the -(void)viewDidLoad of the viewController that you want to force portrait for do the following:
UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
[viewController release];
This basically forces portrait, by presenting a controller (which only supports portrait by default).
You should use one orientation for all view controllers. Otherwise you may have some problems. For example in your case imagine that the user has switched to the second view and has changed the orientation to UIInterfaceOrientationPortrait. Now he returns to the first screen and all buttons are on the wrong positions! Actually the problem is that the orientation will not change to UIInterfaceOrientationLandscapeRight back as you expect. It happens because shouldAutorotateToInterfaceOrientation is not called after viewWillAppear is triggered. So you have to code some dirty hacks to force orientation change. It is not a good solution.
By the way, how do you change your controllers? If you are using addSubview, then your second controller's shouldAutorotateToInterfaceOrientation will not be triggered at all. When using addSubview you should handle it by yourself.

UINavigationController navigation stack problems in landscape mode

I have an iPhone application that I am currently converting to a universal binary to work with the iPad. I have successfully implemented everything I need in terms of layout so that full landscape functionality is now supported in my app (previously I primarily used portrait mode to display content).
But, I have one strange problem, and it ONLY occurs in landscape mode: when I push a view controller onto the stack, it takes two taps on the back button to return to the previous view controller! The first tap shows a blank view, but with the same name on the left-side back navigation button, the second tap takes the controller back to previous view like it should.
I don't have an iPad to test, so I am relying on the simulator. The problem does not show up on the iPhone and doesn't show up if you rotate back to portrait mode.
My app consists of a tabbarcontroller with navigation controllers loaded for its vc's:
//application delegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
//....
WebHelpViewController *vc8 = [[WebHelpViewController alloc] init];
UINavigationController *nv8 = [[UINavigationController alloc] initWithRootViewController:vc8];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:nv1,nv2,nv3,nv4,nv5,nv6,nv7,nv8,nil]];
To implement landscape capability, the UITabBarController is overridden to autorotate when required:
//CustomTabBarController.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return [[(UINavigationController *)self.selectedViewController topViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
... works fine. I navigate into new views using this method
SomeViewController *vc = [[SomeViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
Is this only a simulation error? How do I fix this problem?
It sounds like another ViewController is responding to:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
Check this first.

how can I call a screen with a button action (xcode)?

I am developing a Window Based app for iPhone. I use Interface Builder to build Interface. I want to call a new screen with a Button Action. How can I call the screen with Button action ?
By pushing the new controller onto the top of the stack of windows. For example:
EnterNameController *newEnterNameController = [[EnterNameController alloc] initWithNibName:#"EnterName" bundle:[NSBundle mainBundle]];
[[self navigationController] pushViewController:newEnterNameController animated:YES];
Apple has an extraordinary amount of sample code, and this question (as well as many others) could easily be solved by simply visiting Apple's iPhone dev site.
iPhone Dev Site
If you are using a navigation controller, push it onto the navigation controller's stack, as alamodey suggested.
If you want it to be a modal controller (that is, slide up from the bottom and cover the previous controller's view, like the Bookmarks screen in Safari), present it as a modal view controller:
[self presentModalViewController:myNewController animated:YES];
When you want to bring the old controller back, dismiss the modal view controller. From inside the modal view controller:
[self.parentViewController dismissModalViewControllerAnimated:YES];
If you don't want to do either, just remove the current controller's view from the window and add the new controller's:
UIView * containingView = self.view.superview;
[self.view removeFromSuperview];
[containingView addSubview:myNewController.view];
If you go this route, you may want to look into +[UIView beginAnimations:context:], +[UIView setAnimationTransition:onView:], and +[UIView commitAnimations] (if I recall the method names correctly--check the documentation) to animate the transition. You should almost always animate any switch between screens in iPhone OS.
(work in .m class)
#import "classtocall.h"
- (IBAction)ButtonPressed:(id)sender
{
classtocall *mvc = [[classtocall alloc]initWithNibName:#"classtocall" bundle:nil];
[self presentModalViewController:mvc animated:NO];
}
(for window based application)
define in .h class
- (IBAction)ButtonPressed:(id)sender;
where "classtocall" is the class you want to call.
you just need to download sample applications from XCode help. Try Elements and UIcatalog. There are also other - type 'pushViewController' or 'addSubview' adn 'makeKeyAndVisible' in help and download samples
nextscreenViewController *login = [[self storyboard] instantiateViewControllerWithIdentifier:#"nextscreenidentifier"];
nextscreenidentifier.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController: nextscreenidentifier animated: YES];