开发者

iPad: How to display a different screen depending on orientation (landscape / portrait)

开发者 https://www.devze.com 2023-03-20 00:44 出处:网络
I have an iPad application that can be used in all four view modes (portrait up/down and landscape left/right). But at a certain point I have a View that I only want to be seen in landscape mode. So I

I have an iPad application that can be used in all four view modes (portrait up/down and landscape left/right). But at a certain point I have a View that I only want to be seen in landscape mode. So I do the fo开发者_如何学Pythonllowing in the UIViewController that will trigger the action to view the landscape-only view:

- (void) showProperty:(Property *) property {
    if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft || [self interfaceOrientation] == UIInterfaceOrientationLandscapeRight) {
        PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:@"PropertyViewController" bundle:[NSBundle mainBundle]];
        propertyView.property     = property;
        [self.navigationController pushViewController:propertyView animated:YES];
        [propertyView release];
        propertyView = nil;
    }
    else {
        RotateDeviceViewController *rotateView = [[RotateDeviceViewController alloc] initWithNibName:@"TabRotate" bundle: [NSBundle mainBundle]];
        rotateView.property = property;
        [self.navigationController pushViewController:rotateView animated:YES];
        [rotateView release];
        rotateView = nil;
    }
}

This works fine and thus shows either the desired screen (PropertyViewController) when the iPad is held in landscape mode, and if not it shows the RotateDeviceViewController which shows the user a message that he/she is supposed to rotate the device to correctly view the screen.

So when the user then rotates his/her device to landscape mode I want to show them the right view (PropertyViewController). And all of this kinda works!

The problem arises though in this RotateDeviceViewController.. There I have the following:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
        [self showProperty];
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

- (void) showProperty {
    PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:@"PropertyViewController" bundle:[NSBundle mainBundle]];
    propertyView.property     = property;
    [self.navigationController pushViewController:propertyView animated:YES];
    [propertyView release];
}

So as soon as I rotate the device (when viewing the RotateDeviceViewController) to landscape mode I show the user the PropertyViewController. This works... But when the PropertyViewController appears it shows my layout 90 degrees rotated. So basically it shows the content in portrait mode instead of using the landscape mode (which is actually the way you are holding the device)..

I hope this makes sense and someone can show me what's causing this.

Screenshots to make it more clear:

  • When device is held in portrait mode
  • After rotating the device


At this point

- (BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)interfaceOrientation

You are telling the view controller what orientations you support. The device has not actually rotated yet therefore the view controllers intefaceOrientation property will still be portrait so when it is pushed onto the stack it thinks the device is portrait.

pseudo code
shouldAutoRotate... // at this point self.interfaceOrientation == portrait
                    // you push your controller here so it loads when the property is 

I'm not sure if this will work well but the earliest I can see you can push is in

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
0

精彩评论

暂无评论...
验证码 换一张
取 消