In my Application I'm using a navigation controller to mange views. My login page support both portrait and landscape views. When user logged in my second view is home and it support only landscape mode. What I want to do is when user login to the home using portrait view home page should appear in landscape view even though the device in portrait.
So what I did was I change the status bar orientation in to landscape int the home page's viewWillAppear method as follows;
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:NO];
UIDeviceOrientation orien = [[UIDevice currentDevice] orientation];
}
also I have override the shouldAutorotateToInterfaceOrientation as follows
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
My problem is even the status bar changed to landscape my UIViewController (home) is remains in landscape mode. When i'm debugging I found that even I change the status bar orientation to landscape,[[UIDevice currentDevice] orientation] returns portrait. I went through internet whole day. And implement lot of solutions proviede by other but my whole day wasted. can some one guide me to solve these issue.
you just need to like:-
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
else
{
return NO;
}
}
for particular class you want to open landscape Only
in ios6:-
-
(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
Apple does not want you to force the orientation of the device. There is a trick though.
Unfortunately I do not have access to my code.
1. Your app in general supports all orientations.
2. All view controllers only return their supported interface orientation in their overwrites respectivly (in supportedInterfaceOrientations).
3. All view controllers return YES in shouldAutorotateToInterfaceOrientation only for their supported orientations.
That is fine. But it would still require the user to actually rotate the device. Otherwise the whole orientation change mechanism would not be invoked.
Now, when you want to force the orientation change, do the following:
4. Use setStatusBarOrientation to set the orientation before the next view controller is displayed.
That alone would not do anything. Plus it would not take any effect if the next view controller is pushed. It would work fine only when the next view controller is presented modally.
5a. So if you want to present the rotated view controller modally, then do it.
5b. If you still need to push it then:
5b1. Create an empty UIViewController instance. alloc/init will do.
5b2. Present it modally
5b3. Dismiss it modally
Now, the new view controller was not even visible to the user but the device - here comes the magic - is rotated now.
5c4. Next push the view controller that you want to display roated.
And vice versa on your way back :)
All the above gets more complicated when you use a tab bar. Do you use a tab bar?
I managed to get that working with a tab bar which I had to subclass to overwrite its rotation methods. In an app without tab bar I subclassed UIApplication (!) but don't rembember wether that was really required or wether I did that out of convenience (instead of aplying the changes to 50+ view controllers). But in principle the above is it that does the trick.
PS: You find a more detailled answer here along with code samples:
Presenting Navigation Controller in Landscape mode is not working ios 6.0
You can try with
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
and
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
or try presenting it as a Modal, rather than pushing it.
Related
I am looking to have one view in my app have landscape orientation. I have managed to get the view to stay in landscape when rotated manually, but if the device is already portrait, it stays portrait, regardless of its supported orientation (set using supportedInterfaceOrientations method) . Is there a way to get the view to rotate automatically? I have tried:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
but this doesn't work.
Any suggestions would be greatly appreciated!
One way to do this is by overriding preferredInterfaceOrientationForPresentation but in order for that to be called the viewController has to be presented (as in modal) and not pushed as mentioned here:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
NSLog(#" preferred called");
return UIInterfaceOrientationPortrait;
}
In order to present your viewController in a UINavigationController use:
UINavigationController *presentedNavController = [[UINavigationController alloc] initWithRootViewController:protraitViewController];
[self presentViewController:presentedNavController animated:YES completion:nil];
To make UINavigationController respect your current viewController's orientation preferences use this simple category instead of sub-classing.
Also, this part of Apple's documentation is a good read for understanding iOS orientation handling better.
Define the following in the UIViewController for your landscape-only view:
- (UIInterfaceOrientation) supportedInterfaceOrientations
{ return UIInterfaceOrientationLandscapeLeft; } // or right or both
This should prevent your view from ever appearing in portrait. From iOS documentation:
Declaring a Preferred Presentation Orientation
When a view controller
is presented full-screen to show its content, sometimes the content
appears best when viewed in a particular orientation in mind. If the
content can only be displayed in that orientation, then you simply
return that as the only orientation from your
supportedInterfaceOrientations method.
Yes, I know this question has been asked a lot of times, but I can not find anything that helps me any further.
Using a navigation controller with 3 viewcontrollers, I need to retain the data from previous screens so I do not use segues but like this:
// When button is pressed
- (IBAction)changeView:(id)sender {
NSLog(#"Skipped connection screen");
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondView"];
[self.navigationController pushViewController:vc animated:YES];
}
where SecondView is the identifier of the view controller that should appear. Since I only want the rotation to be at horizontal right, I add this snippet of code at the top of every .m file I have for my views:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
and in my project-Info.plist I have added Initial interface orientation = Landscape (right home button and in my project settings I have added support for this orientation only.
The problem is that when running on the iPhone, the orientation changes from landscape if I turn my phone either way. When I try to turn it back it just wont. I would like to make sure this application is never able to rotate away from landscape right.
Any suggestions? Thank you very much in advance.
I think if you will add below key to your .plist file then it will be fixed.
"Supported interface orientations"
Value of this key would be "Landscape (right home button)" or whatever you want so your application will support only that specified orientations.
Also add this code into every view controller.
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
You should use the argument in your 'return' code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
This one has me ripping my hair out. My iPad application is setup as follows:
In my app delegate I have this:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
in my info.plist I have:
Initial Interface Orientation = UIInterfaceOrientationLandscapeLeft
In my first view controller that is loaded from the app delegate I have:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
In my second view controller I have:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Finally the views in both nibs are set to landscape in Interface Builder. The application starts in landscape mode and there is a button that when pressed assigns the second view to the first view:
self.view = secondView.view;
the problem is that even though everything is in landscape the new view is always loaded in portrait mode? Please any help on this would be very very appreciated!
This is the bit you want to change. This is what sets the allowed initial orientations. Change it to suit your needs.
It's in the summary tab of the Target's settings.
Or. if you aren't using Xcode 4 yet - you can set the orientations in the Info.plist:
And so you can see all the keys:
I have two views, on first view i have multiple buttons, i wanted that view to be landscape right so i put this code :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
it works fine as my application starts in landscape right mode. the buttons on this view pushes user to second view which i wanted to be a portrait mode. so i put that code in second view but its not working as i wanted.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
its showing my the orientation which i had on previous view. should i have to put something on viewdidload to make it works? please help. thanks
It's a bit a hack, but here it is. In the -(void)viewDidLoad of the viewController that you want to force portrait for do the following:
UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
[viewController release];
This basically forces portrait, by presenting a controller (which only supports portrait by default).
You should use one orientation for all view controllers. Otherwise you may have some problems. For example in your case imagine that the user has switched to the second view and has changed the orientation to UIInterfaceOrientationPortrait. Now he returns to the first screen and all buttons are on the wrong positions! Actually the problem is that the orientation will not change to UIInterfaceOrientationLandscapeRight back as you expect. It happens because shouldAutorotateToInterfaceOrientation is not called after viewWillAppear is triggered. So you have to code some dirty hacks to force orientation change. It is not a good solution.
By the way, how do you change your controllers? If you are using addSubview, then your second controller's shouldAutorotateToInterfaceOrientation will not be triggered at all. When using addSubview you should handle it by yourself.
I have a case where a portrait only mode view needs to be pushed into a navigation controller which has a landscape mode view as its current view.
Although I have reimplemented
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
but it is still pushing the view in landscape mode. I have also tried the following way, but even this is not working.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
}
I tried looking into other related questions but did not find anything.
Any idea how I can solve this?
Thanks!
Your best bet is to just use a modal view controller instead which forces the view to be in portrait regardless of device orientation.
If you want to wrap a navigation controller around it to keep styling consistent, look at Apple's Core Data Recipes example for how they do this when you add a new recipe.