I am trying to push a portrait only view controller onto a controller that allows portrait or landscape. The issue I'm having is that if the user is in landscape mode and I push the new controller on it will remain in landscape mode and just look all screwed up. How do I force the orientation to change to portrait as the new view controller is pushed on?
KDaker's answer is correct but another option you can think about is whether you can limit navigation when your orientation isn't what you want. This isn't necessarily a good idea but there are situations where it can work well. An example would be if you had a video which when rotated to landscape became full screen and covered your navigation back button until it returns to portrait.
in iOS 5 and anything before you cant 'force' an orientation from one view to another. You can support only one orientation for the project but then allow autorotation. So in your case, you can only allow autorotation and wait for the user to rotate the device.
In iOS 6, they have changed the way orientation handling works, and its become alot more flexible. You now have these methods:
- (NSUInteger)supportedInterfaceOrientations
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
Using these, you can present your view controller in any orientation you prefer, given that it is supported in the former method.
hope this helps.
Related
I am developing app using UISplitview.
When I am Rotate the device form Landscap to Portrait I set the frame but It is not set.
In portrait mode only detailview is there. Landscap mode master and detail view is there.
So how can i Identify rotation in portrait mode ?
is it any kind of method is there?(like In view controller has willAnimationRotationtoInterfaceorientation)
from what I understand, you want to be able to see the master in the portrait view.
The design of UISplitViewController specifically hides master view in portrait mode.
There are two ways to go about the situation here. First of all, you declare your ViewController controller as the UISplitViewControllerDelegate. (which view controller as the delegate? the one that stays in the stack - for you to figure out whether it's master or detail).
Then you have a few delegate methods to take a look at.
If you want to straight up just show the master in portrait mode in the following delegate method:
- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation
you return NO (aka you don't want it to hide the view controller)
By default, this is no for landscape, which is why you see both views, and yes for portrait, which is why you don't see the master.
Saying that, the more elegant solution would probably be to hide the master view and then put a button at the top of the navigation bar, after clicking on which, you will be shown the master in portait mode. And upon clicking outside the master view, the master will be hidden.
To do this, you do not use the above BOOL method, but implement these other two delegate methods instead:
– splitViewController:willHideViewController:withBarButtonItem:forPopoverController:
– splitViewController:willShowViewController:invalidatingBarButtonItem:
Search documentation for UISplitViewControllerDelegate.
Make a new project in Xcode using the Master-Detail Application Template for iPad. This project template creates a UISplitViewController that responds to interface orientation. Thus it shows you fully and precisely how to do what you're asking to do.
I have a multitab application where I want the display orientation the same for one tab and dynamic for the other, that means I need for example for
Tab1 = always portrait orientation
Tab2 = automatic detection plus orientation
I have tried to disable the orientation on Tab1 but that will disable the orientation in the whole app.
Any help?!
From Apple docs:
"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."
So the answer is no you cannot do what you are trying to do, it's an all or nothing kind of situation.
If you have not already used, I think calling different UIView for different Tab will help you to do so.
If you have used it, then making 2 different ViewControllers and handling the same thing different may help you to do so.
I am working on an iPhone app with a UINavigationController interface and there I want all views to ONLY allow default portrait rotation except for one. I know I can return 'NO' in the shouldAutoRotate method but basically when I am in the view that does allow rotation and go back to the previous view, the other views are then stuck in landscape as well. Any ideas?
you need to change status bar orientation when you go back to previous view.
Update: Solved. See fix at bottom.
In my iPad app which supports all orientations, if the app is loaded in landscape, weird things happen:
Alert views appear in portrait, even though the rest is in landscape
Rotating the iPad from landscape to portrait does not trigger a rotation (nor does it trigger any of the shouldAutorotate style methods).
Rotating the iPad 180 degrees (to "other landscape") does trigger rotation, and after this, the iPad behaves like normal (i.e. rotates correctly to portrait and landscape when rotated).
Alert view screen below. Weird, weird. Any ideas why this might be happening? It only happens when loading in landscape -- loading in portrait and then rotating to landscape works just fine.
Solution: This problem occurred because I was adding a UIViewController (modally) on top of the main view controller as soon as the app finished loading (more specifically, in the app's viewDidAppear: method).
iOS sends a number of (4) shouldAutorotateToInterfaceOrientation: calls to the current/main view controller after things have started up, to figure out which orientations are currently supported. When I added the other VC so soon, this occurred in the middle of the aforementioned calls. Thus, I only got one or two calls (for "portrait", because it starts with portrait).
The main view controller was then left with a default support of "NO" for the landscape orientations, but the newly added view controller (which is also asked about orientations) now happily responds yes.
If I had removed the other VC (which is in landscape), I would have realized that my main view controller was stuck in portrait. So I had a portrait view, on top of which there was a landscape view. The alerts and such heeded the orientation of the former, hence the disastrous results above.
Additionally, if I tried to rotate to portrait, I couldn't, because the main view controller was already in portrait. Rotating to landscape was also impossible because the other view controller is already in landscape. Only when I did the "landscape but that other one" rotation, did both view controllers agree that rotation was required, and thus the device rotated.
The fix in my case was to simply put the creation of the other controller at a slight delay. using performSelector:withObject:afterDelay. -- 0 seemed to work but I just put it at 1 to be sure. It looks a bit weird, with the main vc appearing and then the other vc, but it's better than the above.
This problem occurred because I was adding a UIViewController (modally) on top of the main view controller as soon as the app finished loading (more specifically, in the app's viewDidAppear: method).
See above post for details on solution.
Use UIAlertController instead of UIAlertview.
I'm strugging with getting an iPhone application which requires just about every push or pop in the Nav Controller Stack to change orientation.
Basically the first view is portrait, the second landscape the third portrait again (Yes I know this is less than ideal, but that's the design and I've got to implement it).
I've been through various advice on here....
How do I detect a rotation on the iPhone without the device autorotating?
Force portrait orientation on pushing new view to UINavigationViewController
Is there a documented way to set the iPhone orientation?
But without total success.
Setting to link against 3.1.2 my reading of the linked articles above seems to indicate that if my portrait view pushes a view with
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) );
}
Then then that view should appear rotated to landscape. What happens is it appears in its "broken" portrait form, then rotates correctly as the device is turned.
If I pop the controller back to my portrait view (which has an appropriate shouldAutoRotate...) then that remains in broken landscape view until the device is returned to portrait orientation.
I've also tried removing all the shouldautorotate messages, and instead forcing rotation by transforming the view. This kind of works, and I've figured out that by moving the status bar (which is actually hidden in my application) [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight; the keyboard will appear with the correct orientation when desired.
The problem with this approach is that the status bar transform is weird and ugly when you don't have a status bar - a shadow looms over the page with each change.
So. What am I missing.
1) Am I wrong in thinking that in 3.1.2 (or possibly earlier) shouldAutorotateToInterfaceOrientation should provide the desired orientation simply by pushing controllers ?
2) Is there another way of getting keyboards to appear in the correct orientation.
3) Are the undocumented API calls the way to go (please no!)
You shouldn't use [UIViewController shouldAutorotateToInterfaceOrientation:] to trigger an orientation change; it's only there to let the system know if automatic rotations are allowed. You should still update it to specify the orientation that's allowed though.
If you want to change the orientation when a particular view is showing, you should call [UIApplication setStatusBarOrientation:animated:] inside your [UIViewController viewWillAppear:] override method for each of the view controllers that force a particular orientation. That will cause a change when a view is being pushed onto the stack and when it's being popped off it. Make sure you call super in your override method.
This is also the right place to change how the status bar is displayed, if that's something you're doing.