On iPhone - my first ViewController supports all interface orientations. Initial behavior though, is when I launch the app the main ViewController should always be in portrait.
In order to overcome situations where the phone was in landscape while the app was launched, I created a dummy ViewController which only supported portrait orientation, launched the app from it and immediately pushed the main ViewController. Indeed the main ViewController was in portrait orientation, but when performing:
self.interfaceOrientation
The result was landscape orientation (like the phone's orientation).
I would expect it to return portrait, and this is causing problems.
How can I get the true orientation of the ViewController and not the device in this case?
You need to override your UINavController to set the orientation in portrait when you want. It's that Vihba means.
The problem is that navController keeps orientation of last view...
**Use this code *****
Also you can refer following link:
How to force a UIViewController to Portrait orientation in iOS 6
#implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
Related
I'm utilizing a navigation controller in my app. I want all of my viewController to be in portrait only except for one, which will support landscape.
Going with an accepted answer in Stack Overflow I subclassed my nav controller and include this code:
iOS 6 - Navigation Controller Landscape Rotations For Some Views While Others Portrait Only
- (BOOL)shouldAutorotate {
id currentViewController = self.topViewController;
if ([currentViewController isKindOfClass:[IssueViewController class]])
return YES;
return NO;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
This singles out the one view controller I would like to rotate. Everything works great except when I launch the app in landscape. The image gets crushed and off centered.
screen shots here: http://imgur.com/a/jyrJ2
Any suggestions on correcting this? Thank you!
Tested your code. This works fine only when you change the orientation, but it doesn't reflect changes according to current interface orientation if you launch the app in landscape or go from one to other controller in landscape.
Use the following approach, which I have used to solve same kind of issue some time ago.
Add this code in UINavigationController subclass
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
And in you ViewController, add this code
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
//Control UIInterfaceOrientationMask here to tell which interface orientations you want to support for this ViewController
return UIInterfaceOrientationMaskPortrait;
}
I have used the below code to force the view to landscape.
objc_msgSend([UIDevice currentDevice], #selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);
Is apple approve this for orientation or have any chance to reject my app.Please provide your solution.
I do not think so. Apple documentation state that orientation is read-only property.
To force landscape into ViewController, you can do:
- (NSUInteger)supportedInterfaceOrientations;
{
return UIInterfaceOrientationMaskLandscape;
}
To present the ViewController in a specified orientation mode:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
{
return UIInterfaceOrientationLandscapeLeft;
}
For all these to happen, the plist of the project must support the orientation and:
- (BOOL)shouldAutorotate;
{
return YES;
}
All these is assuming the view controller is not presented under same UINavigationController. For such case, you should refer to this discussion on work around of forcing rotation on UINavigationController: UINavigationController Force Rotate
For views that you want to force into landscape, you can always use transform:
self.view.transform = CGAffineTransformMakeRotation(M_PI/2.0);
My app has this main structure: UITabBarController, where each tab hosts a UINavigationController that pushes views according to user interaction. Almost all the app is portrait orientation only, except a few specific sections.
The behavior I want is to have landscape orientation when I push one of the sections (let's say a map). In that map, the user can go back to portrait orientation. When he goes back in the main menu, he should go back to portrait orientation (landscape should not be allowed here). I don't mind if he is temporarily in landscape.
Here's some code from my UITabBarController subclass:
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
I use the same code in my UINavigationController, and in the specific menu, I have this:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
if (!UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
return NO;
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
The behavior works almost all the time. When the orientation of the menu is landscape and the user rotates his device, he goes back to portrait, but can't go back to landscape.
My issue is: once in a while, the status bar will rotate, but not the view controller. (see picture) Each time, shouldAutorotate is called and returns the correct value (ie. it returns YES but does not autorotate).
I know this is stretching the SDK, but I'd love to make this work all the time. Any hints?
I hold device in landscape mode and moved into second view controller, which only support portrait mode. when return back from second view controller(Support all orientation), First view controller is not rotating to landscape mode automatically.
if i did below code then its work in IOS6. but not working for IOS7.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
[UIViewController attemptRotationToDeviceOrientation];
}
in IOS7 viewController is rotating but status bar is not rotating
Implement the following in VC A:
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
The solution presented by AMayes is not quite correct.
The UIViewController docs state the following:
Typically, the system calls this method only on the root view
controller of the window or a view controller presented to fill the
entire screen; child view controllers use the portion of the window
provided for them by their parent view controller and no longer
participate directly in decisions about what rotations are supported.
Therefore, put the following code in the UINavigationController subclass:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
So I am developing an iPad app that supports only landscape mode except for on one modal view controller. The issue I am having is that once I present the modal view and change the orientation to portrait then dismiss the view, the parent view (which should only support landscape) is in portrait mode until I rotate the device in which it then goes back to landscape and stays that way. I have been beating myself up trying to figure out how to keep the parents view original orientation but haven't been able to find a solution.
I have the following code in my app delegate to allow orientation changes on only that single modal view (GalleryPhotoViewer) :
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
//Support Portrait mode only on Photoviewer
if ([[presentedViewController presentedViewController] isKindOfClass:GalleryPhotoViewController.class] ) {
orientations = UIInterfaceOrientationMaskAll;
}else{
orientations = [presentedViewController supportedInterfaceOrientations];
}
}
return orientations;
}
From the parent class (PhotosViewController) I am calling :
GalleryPhotoViewController *gpView = [GalleryPhotoViewController new];
[self presentViewController:gpView animated:YES completion:nil];
Also in my parent (and other views) I have the following code to disallow portrait mode :
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
} else {
return NO;
}
}
Any ideas on how I can keep the orientation on my parent view? I was thinking about possibly just programmatically changing the orientation on the parent in the viewWillAppear method once the modal is dismissed but then I wouldn't know what the previous orientation was, not to mention I haven't been able to find code to do this regardless for ios6.
EDIT/SOLUTION : So I found a solution and what I ended up doing was leaving the application:supportedInterfaceOrientationsForWindow: code and just adding the UINavigation subclass to the parent view that was presenting the modal view and everything worked as expected and the parent retained its original orientation while the modal was able to change freely.
In my parent :
//To make sure that this view remains in Landscape
#implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
#end
Thanks #matt for the suggestions.
I think the problem is your use of application:supportedInterfaceOrientationsForWindow:. Instead, get rid of that, and start with a UINavigationController subclass and make that the class of the root view controller that is your navigation interface. Then:
In the UINavigationController subclass, return UIInterfaceOrientationMaskLandscape from supportedInterfaceOrientations.
In the presented (modal) view controller, return UIInterfaceOrientationMaskAll from supportedInterfaceOrientations.