How to show scene in landscape mode within xcode storyboard? - iphone

I have storyboard project in xcode. Most scene is show in portrait orientation but I want to show one of scene in landscape mode without user rotate the device, always this scene is shown it must be in landscape. This scene has a simple viewcontroller with uiwebview object wich I open a pdf file from url. My idea is show the viewcontroller as a modal viewcontroller.
Anyone known how solve this issue?, yesterday I spend my time searching about this in Internet but I don't find out a solution.
Thank you in advance!

How the scene is actually displayed is not determined by the storyboard but rather the viewcontroller. You need to create an UIViewController subclass and implement - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation to return YES only if the interface orientation is a landscape orientation. You could do it like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
to allow both orientations or replace with
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft
to allow only one direction.

Related

Navigation Controller (and all its views) are portrait only. How can I make them landscape only? [duplicate]

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.

iOS cannot make UIWindow landscape in Interface Builder

I am trying to set a UIWindow in MainWindow.xib into landscape mode. Unfortunately, this option is greyed out in Interface Builder. I have a Navigation Controller within the same NIB that can be set to landscape, but this ends up looking awkward in Interface Builder, as the Nav Controller is set to landscape but the containing window is in portrait.
What's worse is that I can't get the window to run in landscape during runtime. I have this code in the view within the nav controller:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
I don't know how to specify this for the containing window though. I have set the orientation in the plist but this doesn't seem to make my view display in landscape.
From what i can see, it seems like you want your first view controller to be in landscape mode, when displayed.
For this, you need to set the "Initial orientation" in your info.plist file. There is a key for this.
Thus, what you need to do is, Make your xib in landscape mode, implement the method as you have done above and set the initial orientation to landscape in plist.
I faced a problem with the UIWindow being forced into portrait mode as well. My solution was to use another view level between the window and the views I was manipulating. Though not a perfect solution it worked. I hope you get it worked out.

iPad & iPhone orientation: when I click my second view and rotate and then switch back to the first view my controls are not repositioned

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.

Always open a view in portrait mode in view in ipad?

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.

MPMoviePlayerController re-orientation portrait to landscape and back to portrait (iOS 4.1)

Since iOS 3.2 the MPMoviePlayerController class allows to embed a movie in the view hierarchy.
Now I'm facing this issue: I create my portrait view by placing an instance of MPMoviePlayerController. When the user touch the "fullscreen" button this view enters in fullscreen mode but the view remains in portrait. When the user rotates the device the fullscreen movie view is not auto-rotated as my app forbids landscape interface orientation.
So in order to allow auto-rotation of movie player fullscreen view, I changed my view controller shouldAutorotateToInterfaceOrientation: method to return YES for landscape if - and only if - the movie player is in full screen mode.
This works perfectly: when the user enters in full screen and then rotates to landscape the player is auto-rotated to landscape and fills the entire screen.
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
return(YES);
}
if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
return([movieController isFullscreen]);
}
return(NO);
}
Now the issue arises when I touch the "Done" button in the full screen view while remaining in landscape. The full screen closes and then what I see is my original view autorotated: but I don't want this auto-rotation.
A partial, but not acceptable solution, is to listen for "MPMoviePlayerDidExitFullscreenNotification" and, if the interface is rotated to landscape, force re-orientation to using the undocumented and private function:
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait]
This works but is not acceptable as usage of this method is forbidden.
I tried to force orientation using [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait] but as I'm in a Tab Bar this doesn't work (the UITabBar remains Landscape-sized).
Thanks for your help
You can use a separate view controller for MPMovieplayer. You don't have to override the
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
in the original view controller.
if you are using MPMoviePlayerViewController, everything is already set nicely for you, as the method shouldAutorotateToInterfaceOrientation: will return YES by default. You can use it as a subview or present it modally by calling
presentMoviePlayerViewControllerAnimated:
Check this out: MPMoviewPlayerController fullscreen playback rotation with underlying UIViewController with portrait mode only (rotation disallowed)
Maybe a little different problem, but solution may be the same.