iOS 6 landscape orientation problems - iphone

In iOS, after dismissing a modal view controller (which is being forced to only show in portrait orientation), if the device is being held in landscape mode, the view controller which presented the modal view controller doesn't rotate back to landscape correctly.
The 'willAnimateRotationToInterfaceOrientation' method gets called so I can manage rotation of my subviews etc. but then the actual view controller doesn't rotate to landscape. So my subviews look like they should in landscape, but the interface is in portrait mode.
The annoying thing is that it works fine in the iOS 5 simulator. Ie. After dismissing the modal view controller, the presenting view controller rotates back to landscape orientation.
Has anyone experienced something similar, or have any idea how to approach this?

For IOS 6 u need to handle the orientation with this methods.
- (BOOL)shouldAutorotate; {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations; {
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
else{
return UIInterfaceOrientationMaskLandscape;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; {
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
return UIInterfaceOrientationPortrait;
}
else{
return UIInterfaceOrientationLandscapeLeft;
}
}

Related

Turn screen in landscape mode when iPhone is in landscape

I want to make an iPhone app which shows a view when iPhone is in portrait mode, and ANOTHER when iPhone is in landscape mode.
I know there is many post about that but I don't understant the answer.
In a first time, to understand, I make test with a Tabbed Application, because I have already two views. When I tap on the second screen, I would like my iphone in landscape mode.
(and in the first one in portrait mode).
On Apple website, and stackoverflow, I saw the following code :
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if ((orientation == UIInterfaceOrientationPortrait) ||
(orientation == UIInterfaceOrientationLandscapeLeft))
return YES;
return NO;
}
Or a similar code.
In my mainstoryboard, I put the second view in landscape, with the interface.
But when I run my app, and I tap on second screen, iPhone stay in portrait mode..
I tried to do the same thing with a single view app, and created new file (landscapeViewController) with .xib file, but I can't have a godd result!
First, in storyboard, create segues from your portrait view controller to your landscape view controller and vice-versa. Then, in your portrait view controller, do this:
- (void)viewWillLayoutSubviews
{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
[self performSegueWithIdentifier:#"SegueToLandscapeViewController" sender:self];
}
}
In your landscape view controller, do this:
- (void)viewWillLayoutSubviews
{
if (!UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
[self performSegueWithIdentifier:#"SegueToPortraitViewController" sender:self];
}
}
I succeeded with the following code :
-(void)orientationDidChanged: (NSNotification *)notification {
UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(devOrientation)) {
UIStoryboard *main = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
landscapeViewController *landscape = [main instantiateViewControllerWithIdentifier:#"landscape"];
[self presentViewController:landscape animated:YES completion:nil];
}
else if (UIDeviceOrientationIsPortrait(devOrientation)) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
But I can't change transition between View Controller, whereas I can between simple View.
Maybe it's because I have two views controllers linked to the same view controller.h...?

Xcode can't change back to landscape from portrait automatically

I have an iPhone app in which the RootViewController instance starts out in the landscape mode. It supports camera so it changes to portrait mode to take a photo. Once done it comes back to the RootViewController instance but the orientation doesn't change to landscape. I want this to automatically shift to the landscape orientation.
I tried adding
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]
in viewWillAppear method of RootViewController or viewWillDisapper of CameraViewController but orientation doesn't change. How do I programmatically change the orientation?
Try implementing following method in your controller.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
return YES;
return NO;
}
Hope it helps.

Fixing the orientation of view controllers

I have a button in my RootViewController ....
when i click on this button it will navigate to some otherViewController.
but i want that when i click on the Button otherViewController's Orientation should be Landscape
& when i back from this page(otherViewController) i should got again Portrait mode.
lots of Thanks for any help.
Plz help me i m new in Iphone so can't able to handle this.
Note in iOS 6 shouldAutorotateToInterfaceOrientation is deprecated. Instead you should use supportedInterfaceOrientations and return a UIInterfaceOrientationMask.
So for example:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
Here are all the UIInterfaceOrientationMask options you might return:
UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskPortraitUpsideDown
You should also consider the preferredInterfaceOrientationForPresentation.
In general, orientation should only change from portrait to landscape (or vice versa) when the user rotates the device.
However, if you want your RootViewController to always present its view in portrait and your OtherViewController to always present itvs view in landscape, that's easy enough.
In your RootViewController's .h:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
In your OtherViewController's .h:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Although this would probably be better as it would allow both landscape-left and landscape-right rotations:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
The only way I am aware this can be done is by presenting otherViewController modally.
See my answer here: UIViewController loads rotated to landscape but only supports portrait
Maybe it helps.
in viewWillAppear of your new view controller add
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscape]
and in viewWillAppear of your root viewController add
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
It will cause warning but it works.

Rotating all the views of a TabBarController

I have a TabBarController app with 4 views.
I've put on all the 4 viewControllers the autorotate method returning yes :
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
Thanks to this, now when i rotate the iPhone the rotation works, and even the TabBar is rotated.
In every single view i can use the following code to update to view when the rotation occurs:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
if (orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight) {
//Here i can update the view when it goes to landscape
}
else if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown) {
//Here i can update the view when it goes to portrait
}
}
The problem is that when i make the rotation, only the current displayed view is updating. If later i change view clicking on the tabBar, i see the view not updated for the new orientation.
Is there a way to make all the views being aware of the orientation changed?
Ok i found it! =D
The trick is using the function:
- (void)viewWillAppear:(BOOL)animated
Just call it in your viewControllers, and insert inside the code to update the view. Enjoy!

iPhone - allow landscape orientation on just one viewcontroller

I have a navigation-based application in which I would like just one of the viewcontrollers to support landscape orientation. For that viewcontroller (vc1), in shouldAutorotate, I am returning YES for all orientations and in the other controllers I am returning YES only for portrait mode
But even then if the device is in landscape mode and I go to the next screen from vc1, the next screen also comes up rotated in landscape mode. I assumed that if I return a YES only for portrait mode, the screen should show up only in portrait.
Is this the expected behavior? How do I achieve what I am looking for?
Thanks.
You can't support the landscape orientation only for one of the viewcontrollers if you use shouldAutorotateToInterfaceOrientation method of UIViewController.
You have only two choice whether all viewcontrollers support the landscape or no viewcontrollers support it.
If you want to support the landscape only for one, You need to detect device rotation and manually rotate views in the viewcontroller.
You can detect the device rotation by using Notification.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Then, you can rotate your views when you detect the device rotation.
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
[xxxView setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
} else if (orientation == UIDeviceOrientationLandscapeRight) {
[xxxView setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
[xxxView setTransform:CGAffineTransformMakeRotation(M_PI)];
} else if (orientation == UIDeviceOrientationPortrait) {
[xxxView setTransform:CGAffineTransformMakeRotation(0.0)];
}
}
I have also situation when I need all view controllers in portait mode, but one of them also can rotate to landscape mode. And this view controller has navigation bar.
For this purpose I have created second window, in my case it was camera view controller. And when I need to show camera view controller, I show camera window and hide when I need to push another view controller.
And you also need to add this code to AppDelegate.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (window == self.cameraWindow)
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
As i worked out for my App i advise you to use this solution.By using some conditions in the shouldAutorotateToInterfaceOrientation method orientation type we can solve this.Just try with this link will help you.
https://stackoverflow.com/questions/12021185/ios-rotate-view-only-one-view-controllers-view/15403129#154031