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 );
}
Related
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];
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);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Landscape Mode ONLY for iPhone or iPad
I'm making an iPad-only app that I want to be landscape only throughout the entire thing.
I'm very new to iOS programming and am using the StoryBoard method of creating the interface.
When I first set up the app, I selected a single view and click the buttons to make it be landscape only. I found out that only makes it start in landscape orientation but doesn't prevent the user from manually rotating.
So, I found I had to do:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight)
{
return YES;
}
return NO;
}
in order to prevent the user from manually rotating it.
Now, I've created a Navigation Controller and a regular View that I set as the Root View Controller. I've also added a single button inside that view.
My problem is that my app seems to be landscape only at first startup (literally flashes landscape) but then is portrait only in what seems that navigation controller.
I've also found selected "Orientation: Landscape" for both the Navigation Controller and that first View Controller inside the Storyboard interface.
Even when I rotate the device to landscape, the app doesn't rotate. It seems to be stuck in portrait even though all the settings are for landscape-only.
How can I make my app landscape-only instead of its current state of portrait-only?
EDIT: I actually found my own solution.
It appears as though my view that was linked to my Navigation Controller was not linked to the class file where I was setting the code for landscape only.
All I did was select the View Controller in the Storyboard interface, click the "Show Identity Inspector" button on the right sidebar and set the class as the name of my ViewController files where the code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
was.
In short, my view controller wasn't linked to the view controller class file.
Returning YES and NO is not very recommended.
Returning UIDeviceOrientationLandscapeLeft however should make your app "landscape only":
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
If that don't work, navigate to your Info.plist
and add a Supported interface orientations row with Landscape (left home button) and Landscape (right home button).
Additionally, I recommend to change the view(s) Orientation to Landscape in the
Attributes Inspector.
Set your return value:
return NO;
And if you are using the latest xcode, then set the orientation from the project options. Else set the orientation using the project info plist, Supported interface orientations and set it to Landscape (left home button) -- delete the rest array keys.
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.
When I navigate from one view to another view, I want to open the view in portrait view only.ie I am navigating from a first view (landscape) to second view. I want the second view to always be open in portrait view.In my case when I launch in landscape, the view is in portrait but the device is in landscape mode. The output I expected was if I open the view in portrait and and on rotating it to landscape with no rotation.
EDIT:
If you open the app in portrait and if you given auto-rotate as NO.Then if you rotate the device to landscape,then there will be no rotation in output.I want the same effect when loading a view initially in landscape.
I'm pretty sure you cannot use setOrientation anymore, since it is deprecated and apps have been declined for using it.
This may be an option for you:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/33548-alternative-setorientation.html
It rotates the view using a transformation.
Okay so what you want to do is edit one of the functions in the view file. You want to set;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES;
}
From return YES to return (interfaceOrientation == UIInterfaceOrientationPortrait);
Then your view should only be in potrait.