iOS - View not loading properly in iPad when rotating

I have created a simple iPad application with rotation support. It has two view controllers. First View Controller is loaded automatically as rootViewController of the AppDelegate. It has a button on it which, when clicked, changes the rootViewController of AppDelegate to Second View Controller. The Second View Controller has a button on it which sets rootViewController back to First View Controller. This work Perfectly in Portrait mode. But when I rotate the simulator to Landscape mode, and click on the button on First View Controller to load the Second View Controller, it first displays the Second View according to the device (un-rotated), and then rotates the display to normal (Landscape), after completing the animation. What is going wrong ? The methods in AppDelegate to set View Controllers are as follows:

(void)loadSecondView 
{
    SecondView *secondViewController = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.window cache:YES ];    
    self.window.rootViewController = secondViewController;
    [UIView commitAnimations];     
}


(void) removeSecondView 
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.window cache:YES ];
    self.window.rootViewController = self.firstViewController;
    [UIView commitAnimations];    
}

Answers


Basically you're wrapping two operations inside an animation block.

  1. making a new view controller as the root view
  2. showing that view in a curl animation

As a result, you see two operations during your animation.

  1. new view is added to the window, and it's rotated based on device's orientation
  2. curl animation makes the new view visible

You can fix this problem by applying the following change:

- (void)loadSecondView {
    SecondView *secondview = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
    self.window.rootViewController = secondview;
    self.window.rootViewController.view.hidden = YES;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.window cache:YES ];    
    self.window.rootViewController.view.hidden = NO;
    [UIView commitAnimations];
}

- (void)removeSecondView {
    self.window.rootViewController = self.viewController;
    self.window.rootViewController.view.hidden = YES;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.window cache:YES ];
    self.window.rootViewController.view.hidden = NO;
    [UIView commitAnimations];
}

Basically, we're adding the view without any animation effect, and for transition animation, we're using view's hidden property.

HTH.


Need Your Help

Passing data from view to controller in Laravel

php laravel laravel-4

I understand that passing record ids through the url isn't usually a good idea, but I am wondering how I can avoid it in my case:

How do I Remove or Strip off all the way up to delimiter

java java-ee

I'm trying to modify a response field (Result). The response value looks like this,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.