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);
}
Related
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 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.
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.
In a navigation-based app, LandscapeViewController only supports landscape mode (all others support both modes). I also have a "loading screen" that advises the user to rotate the phone before continuing. This way I can make sure that when my landscape view loads, that it's in landscape mode.
The problem comes when I rotate the phone to portrait mode while still showing LandscapeVC. I press the Back navigation button to navigate up one level (to a VC that supports both landscape and portrait modes), but the upper level shows in landscape mode even though the phone is in portrait mode. I guess this is because when I left this view I was in portrait mode, I then rotated the phone while in another view, so this view has not received the notification. If I then proceed to rotate the phone to the other landscape mode (say the LandscapeVC was loaded on its right side, so I'd rotate the upper VC from portrait to the left landscape mode), it will update.
My question is: how can I notify this upper view that the phone was rotated, so when the user goes up after putting the phone in portrait mode, the upper view shows correctly?
Where you pop the sub-view off the stack and return to the previous view (currently stays in landscape, you'd like in portrait), you could try sending it to the setNeedsLayout selector.
Please try this,
This may help you.
All the best
- (BOOL) shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
return NO;
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft )
return YES;
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}