8

This is just a test application, There is only a AppDelegate class to create all I did was create a Window based app, set the supported orientations to only the landscape in the info.plist, and then add the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];

// Override point for customization after application launch.
UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"hu" message:@"hui" delegate:nil cancelButtonTitle:@"hi" otherButtonTitles:nil];
[test show];
[window makeKeyAndVisible];
    NSLog(@"win %f - %f", window.bounds.size.width, window.bounds.size.height);
return YES;
}

Without the first line, which sets the status bar orientation, the alert view appears in portrait even though the rest of the interface is in landscape left.

Anyway the Log still gives this:

win 768.000000 - 1024.000000

This is the wrong way round (and thus when I add subviews in my real app the frames are not correct)

Apple seems to have really mucked up on the interface rotation, because I've had nothing but problems, I don't remember any of this happening on the iPhone, so please can someone tell me how to fix this.

I'll give 500 reputation (that's all but 10 of my reputation) to the person who can at least explain why this happens and hopefully provide a solution.

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • Which SDK version are you using? – ImHuntingWabbits Oct 28 '10 at 07:44
  • Why are you doing that in the AppDelegate? To get around this I create a BOOL in the AppDelegate which is False until that method is called where it changes to TRUE. Then in the viewWillLoad in my MainVC I start a timer which checks if the AppDelegate BOOL is TRUE. If it is I fire the method that shows the alert. To make it look nicer I also show the Default image on my MainVC until the message appears. This may seem a hacky approach but I don't always have to show the alert view so I check that too. – ingh.am Oct 28 '10 at 09:36

7 Answers7

17

I think the "Launching in Landscape Mode" of the iOS Application Programming Guide mostly explains what is happening with your test application:

Applications in iOS normally launch in portrait mode to match the orientation of the Home screen. If you have an application that runs in both portrait and landscape mode, your application should always launch in portrait mode initially and then let its view controllers rotate the interface as needed based on the device’s orientation. If your application runs in landscape mode only, however, you must perform the following steps to make it launch in a landscape orientation initially:

  • In your application’s Info.plist file, add the UIInterfaceOrientation key and set its value to the landscape mode. For landscape orientations, you can set the value of this key to UIInterfaceOrientationLandscapeLeft or UIInterfaceOrientationLandscapeRight.

  • Lay out your views in landscape mode and make sure that their autoresizing options are set correctly.

  • Override your view controller’s shouldAutorotateToInterfaceOrientation: method and return YES only for the desired landscape orientation and NO for portrait orientations.

Important: The preceding steps assume your application uses view controllers to manage its view hierarchy. View controllers provide a significant amount of infrastructure for handling orientation changes as well as other complex view-related events. If your application is not using view controllers—as may be the case with games and other OpenGL ES–based applications—you are responsible for rotating the drawing surface (or adjusting your drawing commands) as needed to present your content in landscape mode.

In terms of your test application, the key part is the last section. You do not have a view controller, so you are entirely responsible for setting up the UI in the orientation you want. That is why you have to set the status bar orientation manually.

I read the first paragraph as saying that iOS apps always launch in portrait mode and then the root view controller rotates its view to match the device orientation immediately and without animation once it is added to the window. That means the UIWindow itself does not rotate so its dimensions will always be in terms of a portrait orientation (as tadej5553 has said). In addition, the frames of all of the UIWindow subviews will also be in terms of a portrait orientation (since the frame is always defined in the parent view's coordinates). So, no matter how you rotate the device, the root view controller's frame will always be in terms of a portrait orientation. However, since a view's bounds property is defined in terms of its own coordinates, that height and width should reflect the current orientation of the view.

It is not clear what you are trying to accomplish with your real app, but the recommended practice is to lay out your views for portrait orientation and then set their autoresizing properties to handle the automatic rotation (whether it occur immediately after app launch or later).

Tim Isganitis
  • 1,554
  • 9
  • 11
  • Really good answer, so I'll accept it and start another bounty (not of 500 though :\\) when I can and award it to you, however personally I prefer Deniz's answer so he get the bounty. You explain well why it happens in general, which is really useful to me and if I could split the bounty now I would. – Jonathan. Oct 29 '10 at 08:17
  • The iPad is an exception to the rule because it's home screen can rotate to landscape. – bentford Nov 01 '10 at 18:25
10

UIWindow does not know anything about rotation. Rotation methods are encapsulated in UIViewController. (auto rotations etc)

UIAlertView uses UIApplication's statusBarOrientation property to choose alert's orientation.

// Rotate to a specific orientation.  This only rotates the status bar and updates the statusBarOrientation property.
// This does not change automatically if the device changes orientation.
@property(nonatomic) UIInterfaceOrientation statusBarOrientation;

As you can see from SDK headers, statusBarOrientation does not change automatically. So either you or UIViewController (auto rotation) should change this property.

If you start portrait (which is consistent with UIWindow's default orientation), and use shouldAutorotateToInterfaceOrientation and let UIViewController to handle rotation, it changes UIApplication's statusBarOrientation on each device orientation change.

But If you start in landscape mode, you should manually set statusBarOrientation for once.

Hope this helps.

Deniz Mert Edincik
  • 4,336
  • 22
  • 24
  • 1
    Thanks for explaining it terms of UIAlertView and saying what I should do. – Jonathan. Oct 29 '10 at 08:21
  • I have spent so long trying to resolve this before finding this solution. Putting [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; as the first line of my loadView did the job. Thanks! – Andy Jan 25 '12 at 11:05
  • I think I just lost 2h trying to get my iPad app (iOS5) to start in landscape only. This works, thanks a lot. – Jasper Mar 06 '13 at 09:51
6

Use the bounds of the view

tadejsv
  • 2,085
  • 1
  • 18
  • 20
4

Window will always give the size for the portrait orientation. Always.

But the views on it will give the correct measurements (regarding the orientation), so make sure to use those.

tadejsv
  • 2,085
  • 1
  • 18
  • 20
  • I just did this, and well it changes the status bar difference (20 pixels) correctly but it still does not switch the width and the height. – Jonathan. Oct 19 '10 at 20:22
  • The width is still like 768 when in landscape (the height of the screen in landscape is 1024) and vice versa in portrait – Jonathan. Oct 19 '10 at 20:28
  • use the bounds, not the frame of the view to get the correct coordinates – tadejsv Oct 22 '10 at 15:34
3

Instead of manually messing with the status bar, try setting UISupportedInterfaceOrientations to UIInterfaceOrientationLandscapeLeft in your info.plist file. Then the app should be all set up to go once you launch it.

Manually setting the status bar orientation is kind of "low level". Your view controllers also need to return UIInterfaceOrientationLandscapeLeft in the shouldAutorotateToInterfaceOrientation instance method.

Nimrod
  • 5,168
  • 1
  • 25
  • 39
  • Please re read my question :) you'd think what you suggest would work, but it doesn't. – Jonathan. Oct 19 '10 at 19:56
  • Why don't you have any UIViews? You're always supposed to have a container UIView inside your window as far as I know. You're not placing widget views directly inside the window or something are you? – Nimrod Oct 19 '10 at 19:59
  • Well in my true application (the one I talk about in this question is jus trying to get it to work)I have a subclassed UIViewController, and when I add it's view in the App Delegate using window.frame as the view's frame it adds the view with dimensions of portrait mode (so the bottom of the view is cut off and there's a gap to the right) – Jonathan. Oct 19 '10 at 20:07
  • It just refuses to have the window.frame size the correct way round, it should transpose the width and height when the interface rotates but because the windows frame.size is incorrect it means the subview's frame.size is wrong and so then is the subview's subviews. – Jonathan. Oct 19 '10 at 20:09
  • And you do have shouldAutorotateToInterfaceOrientation implemented in the UIViewController? – Nimrod Oct 19 '10 at 20:29
  • Well when I asked the question I didn't have a viewcontroller, but now Tadej's answer maade put one in and implementing shouldAutorotate... doesn't change anything still – Jonathan. Oct 19 '10 at 20:36
  • The last time I created an app like this I only implemented the shouldAutorotate... method and it will launch in the correct orientation and display all the widgets property. I didn't have to do anything with the status bar. – Nimrod Oct 19 '10 at 20:45
3

Here is what I did to get this to work:

  • In the Info.plist, make sure to have these parameters:
    • Initial interface orientation = Landscape (right home button)
    • Supported interface orientations
      • Item 0 = Landscape (right home button)
      • Item 1 = Landscape (left home button)
  • Add a xib based view controller, with a view in it that auto resizes (eliminate the resizing issue, etc.)
    • Note I used a navigation controller here with a view controller because it's what I had handy, but I think this should work with a single VC as well.

Add a little bit of code to your app delegate:

_rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 
_navigationController = [[UINavigationController alloc] initWithRootViewController:self.launcherViewController];
[window addSubview:self.navigationController.view]; //I think this is the key.
[window makeKeyAndVisible];

I think the key is the view, I tried it without it and on iPhone it worked just fine, on iPad though the alert showed up in portrait mode as you say. When I added the view back the alert displayed in landscape mode as desired.

ImHuntingWabbits
  • 3,827
  • 20
  • 27
0

I have had nothing but problems hardcoding my App to Landscape only mode. Alot of the time there was no visual problem but the buttons in the bottom 20 pixels were not able to be selected. I did a clipToBounds on my main UIWindow and added a border on its layer and I could see that the UIWindow was under the status bar. I tried changing my x offset of my UIWindow to be 20 (UIWindow is still in Portrait, its contents are Landscape) but this meant my UIControllers were shifted down by 20 pixels (Not sure why). Although this is a different problem to yours I also found the same problem as you have in that the bounds and frames when printed appear to be correct.

Anyways to cut a long story short I changed my plist from UIInterfaceOrientationLandscapeLeft to UIInterfaceOrientationLandscapeRight and also changed my UIViewController method to:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
  return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

And all my problems went away. Many times I have sworn that there are bugs in Apples rotation code that turn out to be the way I have implemented something but I think this one is definetely an Apple one.

Let me know if this fixes your problem

sjngm
  • 12,423
  • 14
  • 84
  • 114
Brett
  • 1,647
  • 16
  • 34