Transition to landscape rotation inside uinavigationcontroller

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

How can I do it? This is similar to how the Youtube app works when you switch from the movie information screen to the actual movie.

(note: I don't care what rotation the device holds)

+1
source share
3 answers

What appears in the YouTube application when switching from the movie information screen to the actual movie is not a navigation interface - it is a modal view. This always works reliably: if you show the view in the model (using presentModalViewController ), and it can be displayed in only one orientation, the application turns to that orientation.

So, the solution is to not move your landscape view to the navigation controller; present it as a modal view.

Ok, but maybe you want to trick the user into believing that we are still in the navigation interface? Then give a modal look to the navigation interface configured to look like the main navigation interface! Therefore, when you present a modal view, it will be displayed to the user as if the navigation interface has rotated (although there will be no rotation animation).

Now the only problem is that the navigation interface in the modal view does not have a back button if we look at its root view. This violates the illusion (and makes it difficult for the user to return). The solution to this is a trick: double-click on the landscape view in the navigation interface before presenting it as a modal view. Thus, what is displayed in the navigation interface is the second view on the stack, and therefore there is a back button. Then, in the delegate of the navigation manager, catch the "Back" button and release the modal view when the user tries to return to what is known to be the root level. So:

 - (void) doButton: (id) sender { // appear to navigate into a landscape view SecondViewController* sec = [[SecondViewController alloc] init]; sec.title = self.title; // to give the correct Back button title UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:sec]; SecondViewController* sec2 = [[SecondViewController alloc] init]; [nav pushViewController:sec2 animated:NO]; [self presentModalViewController:nav animated:YES]; nav.delegate = self; // so that we know when the user navigates back } // and here the delegate method - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (viewController == [navigationController.viewControllers objectAtIndex:0]) [self dismissModalViewControllerAnimated:YES]; } 
+2
source

Implement the following method, which should be called when the view is inserted into the navigation stack:

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

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

If I am wrong and this is not the case, you can always force the horizontal orientation by setting the orientation of the status bar:

 - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:animated]; } 
+2
source

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

In the previous view, call it from your selected button or cell in the table

 - (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 in a 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]; } 

This is a little "sudden." You cannot place an animation on any of them because it looks too buggy. I suggest that this can be improved by clicking on the intermediate view with animation (a simple black look) and then automatically doing the above things to get to the final landscape.

+2
source

Source: https://habr.com/ru/post/1479097/


All Articles