0

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.

Community
  • 1
  • 1
twbbas
  • 407
  • 3
  • 9
  • 19
  • As i see you updated your question with code form my answer, please accept my answer, as it was helpful and (in my opinion), answered your original question. – Aleksander Azizi Jul 18 '12 at 19:29
  • 1
    Yes, I did. Thank you for that code. Because of that, I accepted your answer and I will leave my edit instead of answering my own question later so your answer will stay the accepted answer. I would also upvote it but I don't have enough reputation yet. Thank you. – twbbas Jul 18 '12 at 19:56

2 Answers2

1

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 Info.plist file

and add a Supported interface orientations row with Landscape (left home button) and Landscape (right home button).

row's

Additionally, I recommend to change the view(s) Orientation to Landscape in the Attributes Inspector.

landscape

user247702
  • 23,641
  • 15
  • 110
  • 157
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
  • I already had the Landscape right/left set in the Info.plist file as the only orientations and it looks like the app flashes as landscape for a second before going into a portrait mode. I also switched the method to including your return statements and it didn't fix the problem. – twbbas Jul 18 '12 at 19:16
  • You question seems confusing, as you say "before going into a portrait mode", do you want Landscape only or Landscape and Portrait on different view's ? – Aleksander Azizi Jul 18 '12 at 19:19
  • I actually found out the solution myself. My view controller wasn't linked to my view controller class file where I put in the landscape-only code. Thanks for your help and I'll answer my own question in 7 hours when StackOverflow lets me post it. – twbbas Jul 18 '12 at 19:23
  • Um you are doing the same thing as returning `YES` or `NO` you just skipped the `if` It is virtually the exact same thing. – Justin Paulson Jul 18 '12 at 19:40
  • No, if you use "if", then it will only go to landscape if your device was rotated to landscape, and the question was how to have the app landscape Only. – Aleksander Azizi Jul 19 '12 at 04:24
0

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.

Panagiotis
  • 1,539
  • 1
  • 14
  • 28
  • I tried that however the Navigation Controller and its Views appear in Portrait-only still. – twbbas Jul 18 '12 at 19:17
  • Ah yes, you have a view that does not support rotation so it blocks your autorotation code. This must be enabled from AppDelegate class also and into any viewcontroller classes present as well. – Panagiotis Jul 18 '12 at 19:19
  • I actually found out the solution myself. My view controller wasn't linked to my view controller class file where I put in the landscape-only code. Thanks for your help and I'll answer my own question in 7 hours when StackOverflow lets me post it. – twbbas Jul 18 '12 at 19:23