views:

221

answers:

3

I want to push a landscape view from within a uinaviagtioncontroller, whose initial view is in portrait... So you click a button, or tableviewcell, and the screen rotates 90° into the new view..

How would I manage that? This is similar to how the Youtube app works when transitioning from movie info screen to the actual movie.

(note: i am not interested in what rotation the user is holding the device)

A: 

Implement the following method, which should be called when the view is pushed onto the navigation stack:

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

This should automatically rotate everything sideways as soon as you push it on to the stack, since the view does not support portrait orientations.

If I'm wrong and it doesn't, you can always force horizontal orientation by setting the status bar orientation:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:animated];
}
Ed Marty
A: 

This is my solution, based on Ed Marty on this page and Simon here - http://simonwoodside.com/weblog/2009/2/27/iphone_programming_how_to_switch/

Within the preceding view, call this from your button or table cell selected method

- (void) launchLandscape
{
    LandscapeViewController *controller = [[LandscapeViewController alloc] initWithNibName:@"LandscapeViewControllerView" bundle:nil];
    [self.navigationController setNavigationBarHidden:YES];
    controller.hidesBottomBarWhenPushed = YES;
    [[self navigationController] pushViewController:controller animated:NO];
    [controller release];

}

And then within the landscape view

- (void)viewWillAppear:(BOOL)animated { 
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
    [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation( degreesToRadian(90) );
    landscapeTransform = CGAffineTransformTranslate( landscapeTransform, +90.0, +90.0 );
    [self.view setTransform:landscapeTransform];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
}


-(IBAction)backButton:(id)sender
{
    [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController popViewControllerAnimated:NO];
}

It's a bit 'sudden'. You can't really put animation on any of it because it looks too glitchy. I guess it could be improved by pushing a intermediate view with animation (a plain black view) and then automatically doing the above stuff to go to the final landscape.

cannyboy