Ive got a simple ios navigation app. My problem is navigating between a viewController that is only allowed to be shown in Portrait to a viewController that must be shown in landscape.
The problem is that the second viewController will not show in Landscape on load. If I rotate the device it will update as it should, but I would hope that since it only supports landscape than it should automatically snap into landscape on transitioning to the view in the first place. Any ideas?
Thanks
You should be implementing shouldAutorotateToFaceInterfaceOrientation:
Put this in the PortraitviewController.m:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
And this in the LandscapeviewController.m:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Related
I have a UITabBar application with embedded UINavigation for some of the views. On one specific navigationview I am displaying graphs/charts and it would be better to display them in landscape as if the iPhone was rotated to the left or right. The rest of the app is better suited to portrait. So I want to "force" the views containing graphs to load in landscape regardless of how the user has the device physically rotated. I've tried:
#pragma mark - Rotation
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
But this does not seem to do anything to the view. If I select the "Landscape Left" icon in the "Supported Interface Orientations" for my target then it allows the entire app to re-orientate on rotation of the device. Is there a way to lock my app in portrait for all normal views and lock in landscape for my views containing graphs such that the app ignores the actual device orientation?
You are right. In tab bar applications it is not the shouldAutorotateToInterfaceOrientation: method of the individual view controllers being called. Only the tab bar controller's shouldAutorotateToInterfaceOrientation is called to determine whether and how the views can be oriented.
However, the individual view controllers should implement shouldAutorotateToInterfaceOrientation accordingly anyway. Those methods are still used to determine the orientation of animation effects when pushing or pulling a view controller.
I never tried the following myself: You could try subclassing the tab bar controller and respond to shouldAutorotateToInterfaceOrientation accordingly depending on which view is currently shown to the user. But I fear that Apple has good reasons for forcing us to support the same orientations for all views within a tab bar app.
Firstly I am using iOS 6 SDK.
I am using a custom TabBar Controller.
and controlling the orientation of that TabBar Controller by the below set of codes
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (BOOL)shouldAutorotate
{
return YES;
}
And the view controller should contain
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return UIInterfaceOrientationIsLandscape(orientation);
}
This is what I did and was able to lock a view to landscape or portrait (in my case). To force may be you can put a alert view stating the user to turn the device to landscape and show him the graph. Just a suggestion.
Try it in particular ViewController in which you need to required in landscape mode:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
I have two views in my application. One (by default) is Landscape. When the user clicks a certain menu, i need a Portrait view to appear.
I have all the code in place and i'm trying to use this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
But the app still displays the next view in Landscape. I can't figure this out. Any help will be great.
You can not do it using navigation controller but if you are presenting your view then it is possible to do that and make sure when you are presenting your view animation should be NO.
It works for me very well.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
You can use following code in your viewWillAppear method to make your view portrait.
[[UIDevice currentDevice] performSelector:NSSelectorFromString(#"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
I am making a view based application. For one of the xib files, I want it to only appear in landscape mode. And When i click a button in the first page it should automatically rotate to landscape and navigate to the new screen which should be in Landscape orientation.
How do i do this ?
If you are using UINavigationController then you will not be able to navigate from portrait to landscape, you will need to present your new view controller modally, not to push it
Check this answer Force UIViewController to only show in landscape mode
In the view that should be in landscape orientation , add this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
I have a simple navigation app that has 95% of all views displayed in landscape mode. With the one view that makes sense to only show in Portrait mode i have inserted the following code in:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
The problem is that when the app navigates to the view (from a landscape orientated view) it does not switch the orientation to portrait, only when the device is rotated will it snap into portrait and stay in portrait. Is it possible to force it to load in portrait mode on load of the view?
Thanks in advane
I think you should go through : shouldAutoRotateToInterfaceOrientation method of UIViewController Class.
This function returns YES if the orientations is supported by your UIView. If you return YES only to the portrait orientation, then the iPhone will automatically be put in that orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
I have application that uses landscape right orientation.
In view controllers I use:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
But, If user locks iphone or ipad to portrait orientation, screen is displayed
as portrait, not landscape. Also, in info.plist file I defined only Right Landscape orientation as
supported one.
Try setting the UIInterfaceOrientation key in your Info.plist to UIInterfaceOrientationLandscapeRight. That should force landscape right orientation on launch.
The problem was in function viewDidLoad where another view controller is pushed into navigation stack immediately. If pushing action is delayed then landscape rotation will start and proceed, and also desired view controller will be pushed properly with landscape orientation.