UIViewAnimation done by a UIViewController belonging to a UINavigationController?

Posted by RickiG on Stack Overflow See other posts from Stack Overflow or by RickiG
Published on 2010-04-10T13:36:45Z Indexed on 2010/04/10 13:43 UTC
Read the original article Hit count: 705

Hi

I have an UINavigationController which the user navigates with. When pushing a specific UIViewController onto the navigation stack, a "settings" button appear in the navigationBar. When the user clicks this button I would like to flip the current view/controller, i.e. everything on screen, including the navigationBar, over to a settings view.

So I have a SettingsViewController which I would like to flip to from my CurrentViewController that lives on a navigationController stack.

I get all kinds of strange behavior trying to do this, the UIViews belonging to the SettingsViewController will start to animate, sliding into place, the navigationButtons moves around, nothing acts as I would think.

-(void)settingsHandler {

    SettingViewController *settingsView = [[SettingViewController alloc] init];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                           forView:self.navigationController.view
                            cache:YES];

    [self.navigationController.view addSubview:settingsView.view];

    [UIView commitAnimations];

}

The above results in the views flipping correctly, but the subviews of the SettingsViewController are all positioned in (0, 0) and after the transition, they 'snap' into place?

Is it because I instantiate and add my subviews in viewDidLoad, like this?

- (void)viewDidLoad {

    UIImageView *imageBg = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
    [imageBg setImage:[UIImage imageNamed:@"background.png"]];
    [self.view addSubview:imageBg];
    [imageBg release];

    SettingsSubview *switchView = [[SettingsSubview alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
    [self.view addSubview:switchView];
    [switchView release];
    [super viewDidLoad];
}

1: How should I correctly do the "flip" transition, from within the UIViewController in the UINavigationController, to a new UIViewController and subsequently from the new UIViewController and back to the "original" UIViewController residing on the UINavigationControllers stack?

2: Should I use a different approach, than the "viewDidLoad" method, when instantiating and adding subviews to a UIViewController?

-question 2 is more of a "best practice" thing. I have seen different ways of doing it and I am having trouble either finding or understanding the life-cycle documentation and the different threads and posts on the subject. I am missing the "best practice" examples.

Thank You very much for any help given:)

© Stack Overflow or respective owner

Related posts about uianimation

Related posts about uiviewcontroller