Set Orientation to Portrait on iPhone - iphone

I have an application that consists of a login, 3 tables, and then an image.
You can rotate the image to landscape mode but what I want to be able to do is when the 'back' button is pushed and the app returns to the previous screen, I was the app to automatically rotate to give a portrait view.
Is there any way of doing this?

For previous controller:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
return YES;
}
return NO;}

If the previous view controller only supports portrait (see shouldAutorotateToInterfaceOrientation:) then it should automatically rotate.
If the previous view controller supports landscape but you want it to rotate to portrait if it was originally in portrait, you can probably force it by changing what shouldAutorotateToInterfaceOrientation: returns when navigating back. I wouldn't recommend this; it's inconsistent UI (and it's a bit tedious to figure out what navigation is going on).
There are various ways to set the view controller interface orientation (-[UIDevice setOrientation:] will attempt to trigger an autorotation), but then you're into the realm of private APIs and potential rejection.

Override shouldAutorotateToInterfaceOrientation: in your table view controller and only return the orientation you want.
This might not actually switch back the orientation, so if this alone doesn't work you might have to call setStatusBarOrientation:animated: on UIApplication in your table view controller's viewWillAppear: or viewDidAppear: methods.

Related

View is not rotating

I am making a navigation based app and I need only portrait orientation except in a ZoomPictureViewController ( Zoom in, zoom out images) that supports all orientations.
I am presenting ZoomPictureViewController and returning YES in shouldAutorotateToInterfaceOrientation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
But I get no rotation. I know that shouldAutorotateToInterfaceOrientation, willRotateToInterfaceOrientation, RotateToInterfaceOrientation are only get called on the current/visible view controller but this is not happening in my case. I have checked it via putting breakpoints and NSLog.
Are you using any type of Navigation Controller or a Tab View Controller? I've noticed that there are issues when rotating a UIView that's not the first or only view as a direct child of the main window.
So if your UIView is part of a Navigation Controller or a Tab View Controller, you'll also need to override shouldAutoRotateToInterfaceOrientation on the Navigation Controller or Tab View Controller.
Also I here's an important gotcha in the Apple documentation that might explain the problem you are having.
Tab bar controllers support a portrait
orientation by default and do not
rotate to a landscape orientation
unless all of the root view
controllers support such an
orientation. When a device orientation
change occurs, the tab bar controller
queries its array of view controllers.
If any one of them does not support
the orientation, the tab bar
controller does not change its
orientation.

UITabBar and View Controller rotation problems

I have a UITabBar with 2 bar items. The initial orientation of the device is portrait. If I rotate the device to landscape while being at tabBarItem2 the whole thing(Status Bar, TabBar, ViewContent2) rotates fine, but when I press the tabBarItem1 the ViewContent1 is still in Portrait. It also happens if I'm in tabBarItem1, then rotate device to landscape and I go to tabBarItem2.
I'm using the willRotateToInterfaceOrientation method on each view controller to move things.
I think this is happening because it is triggering the actual viewController's willRotateToInterfaceOrientation method and not on both of them.
Any ideas on how to fix that?
Both view controllers need to have
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

iPad & iPhone orientation: when I click my second view and rotate and then switch back to the first view my controls are not repositioned

I have an app for the iPad/iPhone and Portrait and Landscape is working just fine. However, I recently added a TabViewController and a second tab with a view. Problem is when I click my second view and rotate and then switch back to the first view my controls are not repositioned
Can anyone tell me what I need to do so that I can reposition my views when the first view is clicked?
incidentally, I am assuming I will have the same problem the other way too... view 2 to view 1.
Did you checked that all your view controller implement this method ?
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
From my experience, the same problem also occurs with navigation controller. I guess that the framework wont send the rotation event to every hidden VC on purpose to save processing time. The solution I ever did is just overriding viewWillAppear and correctly layout subviews there if needed.

Rotate single screen in Navigation Controller and TabBar

I have a Navigation Controller and TabBar. I would like to know how the iPod app handles allowing only the play screen to rotate. I have tried to replicate this on iOS 4.2, and if that one screen rotates to landscape and you navigate back, the old screen is too. This is undesired... is there anyway to make the 2nd screen in rotate while not causing the rest of the screens or any of the TabBars view controllers elsewhere do so?
Thanks.
Make sure your rotatable view returns YES in shouldAutorotateToInterfaceOrientation: method of UIViewController.
Make sure your non rotatable view returns NO in shouldAutorotateToInterfaceOrientation: method of UIViewController.
Subclass UITabBarController, override shouldAutorotateToInterfaceOrientation: and handle rotation here based on current UIViewController's shouldAutorotateToInterfaceOrientation: return value. You have to also check if current view controller is UINavigationController or not and if yes, you have to get current view controller from UINavigationController too.
It's not recommended to do this (I mean subclassing of UITabBarController), but UITabBarController forbids rotation if not all UIViewControllers do allow rotation.
You need to state that in the 2nd screen the device orientation is only the desired screen orientation. So for every controller you create you need to implement shouldautorotatetointerfaceorientation:.

Problems with Interface Orientation and UITabBarController

Quick problem:
I have an UITabBarController with 2 navigation controllers [lets call them Left and Right Controller]
On the default selected Left Controller I can push a new View Controller that detects interface orientation.
On the Right Controller I can push the same View Controller but it won't detect interface orientation, or for that matter, It won't even go into the shouldAutoRotateInterface method at all T___T
Haaalp!!
If it is of any relevance, the View Contoller that I'm pushing use the hidesBottomBarWhenPushed property.
Most likely this is your problem:
Tab bar controllers support a portrait
orientation by default and do not
rotate to a landscape orientation
unless all of the root view controllers support such an orientation.
When a device orientation
change occurs, the tab bar controller
queries its array of view controllers.
If any one of them does not support
the orientation, the tab bar
controller does not change its
orientation.
The solution is to override the following method on every view controller leading to your view:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return YES;
}
For example, instead using the default UITabBarController in IB, replace it with your own subclass containing just the method above.
I'm a bit late to the party on this, but I ran into a problem with autorotation at startup for a tab bar app I wanted always to run in portrait.
The app's plist has the necessary settings to both start in and only allow portrait mode, and all my view controllers only allow portrait mode. Yet, when I started the app holding my iPhone in landscape, the app started in portrait, but then rotated to landscape!
Rather than subclass UITabBarController, I simply overrode UITabBarController's shouldAutorotateToInterfaceOrientation: method using a category on class UITabBarController. I included this code in my app delegate:
#implementation UITabBarController(UITabBarControllerCategory)
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Works beautifully, and is quite lightweight.
does your uitabbarcontroller implement the auto rotate? any child viewcontroller that wants to implement autorotate has to have its parent implement autorotate.