iPhone device orientation problem - iphone

My application is Navigation based.
All views are displaying Portrait mode except Report mode.Report mode display landscape mode when device is rotate landscape.
If device is rotate landscape mode Report view is displaying landscape mode.if report is landscape mode once again rotate in device Portrait mode it will display normal view of current view.
Flow of current action my view display.
From current view is Portrait mode and i am rotating device in landscape mode so getting Landscape mode of Report mode. after two rotate only i am getting current view in Portrait mode. I need to reduce tow rotation . please guide me . How to check condition for after landscape mode of Report if once again rotate i need to display current view in Portrait mode.
Here at ReportViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
#interface CurrentViewController
BOOL isShowingLandscapeView;
Report *landscapeViewController;
#implementation CurrentViewController
- (void)viewDidLoad
{
[super viewDidLoad];
Report *viewController = [[Report alloc]initWithNibName:#"Report" bundle:nil];
self.landscapeViewController = viewController;
[viewController release];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
[self performSelector:#selector(updateLandscapeView) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
[self presentModalViewController:self.landscapeViewController animated:YES];
isShowingLandscapeView = YES;
}
else if(deviceOrientation == UIInterfaceOrientationPortrait && isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeView = NO;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait ); // support only portrait
}

I'm not sure I understand your question, but the code looks like it might have the logic reversed.
In the ReportViewController,
- (BOOL)shouldAutorotateToInterfaceOrientation
is called before the rotation, so it should
return YES for Portrait (YES, allow the view to rotate from Landscape TO portrait)
and return NO for Landscape (NO, do not allow the view to rotate to Landscape from Landscape).
And similarly in CurrentVC - try
return (interfaceOriention==UIInterfaceOrientationLandscape);
Hope that helps.
-Mike

Related

change device orientation compatible for ios6 and ios5

I NEED: rotate device programmatically from portrait to landscape compatible for iOS6 and iOS5.
THE PROBLEM: I cannot use [UIDevice setOrientation] function on ios6.
WHAT I HAVE: the code for iOS6:
- (BOOL) shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
the code for iOS5:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
So, I need write the code that rotate on iOS5:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// [[UIDevice currentDevice] setOrientation] - I cannot use it
// ???
}
I achieved this by set Status bar orientation, try execute in code:
[[sharedApplication] setStatusBarHidden:YES];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
and when you need portrait mode, set status bar to setStatusBarHidden:NOand again set statusbar orientation to portrait UIInterfaceOrientationPortrait.

UITabBar identifying portrait or landscape orientation

While, for the most part, orientation is working properly for my app, I'm having an issue testing on my iPad 1. If I have the device tipped at a relatively low angle, there are times while navigating through the tabs that the tab bar appears in landscape mode, but the page calls a portrait mode uiview and then tries to render it in landscape mode, screwing up my UI.
I'm trying to figure out if there is a method to lock down "if the tab bar appears in landscape mode, always call the landscape UIViews and if in portrait mode, always call the portrait UIView."
On each view controller I've set the following:
- (void)viewDidLoad
{
[super viewDidLoad];
// iPad-specific condition here
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
self.view = self.portraitViewiPad;
}
else {
self.view = self.landscapeViewiPad;
}
}
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// iPad-specific condition here
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
//show portrait XIB here
self.view = self.portraitViewiPad;
} else {
//show landscape XIB here
self.view = self.landscapeViewiPad;
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// iPad-specific interface here
return YES;
}
else
{
// For iPhone and iPod touch interface
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
I've also adjusted the app delegate using the method below thinking that could address the issue:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//CALLS RELOAD METHODS HERE AND EACH UIVIEW IS PROPERLY BEING CALLED
}
UPDATE:
Corrected this issue by checking the orientation of the status bar and displaying the correct uiview accordingly. Here's how I updated my viewDidLoad methods:
if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationLandscapeLeft){
NSLog(#"Left landscape detected");
self.view = self.landscapeViewiPad;
} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationLandscapeRight){
NSLog(#"Right landscape detected");
self.view = self.landscapeViewiPad;
} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait){
NSLog(#"Portrait orientation detected");
self.view = self.portraitViewiPad;
} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown){
NSLog(#"Upsidedown Portrait detected");
self.view = self.portraitViewiPad;
}
I think that you don't want to do test [[UIDevice currentDevice] orientation]. As the documentation notes that value can be different than the actual orientation of your app's UI. I think you'll want to rely on the calls to [UIViewController shouldRotateTo...] and [UIViewController willRotateTo...].

Trying to understand how shouldAutorotateToInterfaceOrientation and UIDeviceOrientationDidChangeNotification

I have an interface that I want to startup in landscape orientation. After startup when the user rotates the device to portrait I am displaying a day view calendar. When returning to landscape orientation the calendar is dismissed. Everything works great in every orientation with my application user interface displaying properly in landscape orientation and the calendar displaying properly in portrait orientation.
The problem is if the user is holding the iPhone in landscape orientation on startup. No matter what I do I cannot get it to startup with my user interface in landscape mode. My UIDeviceOrientationDidChangeNotification method fires twice, the first time [UIDevice currentDevice].orientation is landscape, the second in it is portrait. The end result is the the user interface rotates to portrait mode and displays the day view. Not what I want. I want the user interface to stay in landscape orientation until the user physically rotates the device from landscape to portrait.
I don't understand why it fires with a landscape [UIDevice currentDevice].orientation when the user is holding the device in portrait orientation.
Here is what my code looks like in the viewController...
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ((interfaceOrientation == UIDeviceOrientationPortrait)|| (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) {
return NO;
}
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
showingCalendar = NO;
initializing=YES;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
-(void)didRotate:(NSNotification *)notification {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if ((deviceOrientation == UIDeviceOrientationPortrait) || (deviceOrientation == UIDeviceOrientationPortraitUpsideDown)) {
if ((!showingCalendar) && (!initializing)) {
showingCalendar = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
GCCalendarPortraitView *calendar = [[[GCCalendarPortraitView alloc] init] autorelease];
navigationController = [[UINavigationController alloc] initWithRootViewController:calendar];
[self presentModalViewController:navigationController animated:YES];
}
}else if ((deviceOrientation == UIDeviceOrientationLandscapeRight) || (deviceOrientation == UIDeviceOrientationLandscapeLeft)) {
if (showingCalendar) {
showingCalendar = NO;
if (deviceOrientation == UIDeviceOrientationLandscapeRight){
[self dismissModalViewControllerAnimated:YES];
}else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){
[self dismissModalViewControllerAnimated:YES];
}
}else {
initializing = NO;
}
}
}
I found a workaround to my problem. In viewDidLoad I started a scheduledTimerWithTimeInterval and moved beginGeneratingDeviceOrientationNotifications to the selector method.
Now the notification never fires more than once. The user gets landscape at startup no matter which way the device is being held and after startup all the rotations work perfectly.
Here is my modified code. Everything else stayed the same...
- (void)viewDidLoad {
[super viewDidLoad];
showingCalendar = NO;
initializing=YES;
timer = [NSTimer scheduledTimerWithTimeInterval:.55 target:self selector:#selector(startOrientationNotifications) userInfo:nil repeats: NO];
}
-(void)startOrientationNotifications {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
i wouldn't generate a beginGeneratingDeviceOrientationNotifications,
a simple way could be to use a BOOL to check when portrait is allowed in
shouldAutorotateToInterfaceOrientation
something like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ((interfaceOrientation == UIDeviceOrientationPortrait)|| (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) {
return portraitIsAllowed;
}
return YES;
}
then just change it when needed in other methods .
And keep in mind that shouldAutorotateToInterfaceOrientation is called every time user rotate device AND also when you load (instantiate) your controller the first time

Reverting to portrait after Done pressed on movie player?

I've got a MPMoviePlayer working. It is designed to show a postage-stamp size movie as a subview in the view. When the phone is rotated into landscape, it goes into full screen mode. And when the phone is in portrait it goes into postage-stamp portrait mode.
The only problem is when I press Done when in landscape mode, it stays in landscape, with the postage stamp sized movie, rather than kick back into portrait..
Here's some of my code:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
[moviePlayerController setFullscreen:YES animated:YES];
} else
{
[moviePlayerController setFullscreen:NO animated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
}
How would I get it to kick into portrait mode after pressing Done?
#cannyboy...you just need to use below method in your APPDelegate.m if your application only works with portrait mode
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ([[window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
//NSLog(#"in if part");
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
//NSLog(#"in else part");
return UIInterfaceOrientationMaskPortrait;
}}
I was the same problem and now I am telling you how I solved it. I don't know the below method is right or wrong but it work fine.
Instead of opening MPMoviePlayer in view open it in a new viewController. I mean create a new UIViewController to show the movie and push it some how so that user will not understood that they are redirecting into a new screen.
Disable the parent screen for landscape mode and allow MovieViewController to landscape.
When user press the done button or close button simply pop the viewController and as the previous screen don't support landscape mode so the screen will automatically shows in portrait mode.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_moviePlayerWillExitFullscreen:)
name:MPMoviePlayerWillExitFullscreenNotification object:nil];
- (void)_moviePlayerWillExitFullscreen:(NSNotification *)notification
{
CGFloat ios = [[[UIDevice currentDevice] systemVersion] floatValue];
CGFloat min = 5.0;
if (ios >= min)
{
if (self.interfaceOrientation != UIInterfaceOrientationPortrait)
{
if([self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait])
{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
[self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
[UIViewController attemptRotationToDeviceOrientation];
}
}
}
}
Note that this only works in ios 5.0 and later, and you will get a warning that setOrientation is not supported, but it works pretty well
One way you could try is to force your device to think it is in portrait mode. To do this, try using:
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
If you do not want your device to show landscape while not playing a movie, you will also have to add some logic to your code that you have shown above to only change to landscape if the movie is playing.

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