Search code examples
iosviewcontroller

presentViewController animation from side


I have a single view App and want to show a new ViewController when pressing a nav bar button in the right hand side. I call this VC by this code:

- (IBAction)createEntryButton:(id)sender {
    CreateEntryViewController *vc2 = [[CreateEntryViewController alloc] init];
    [self presentViewController:vc2 animated:TRUE completion:nil];
}

This animation, however, brings the vc2 in from the bottom which seems counter-intuitive according to my UI. So my question is:

How can I make my vc2 appear from the right instead of the bottom with presentViewController?

Thanks.


Solution

  • the cleanest would be to use a navigationController for pushing and popping views..

    if you are already in a NavigationController

    [self.navigationCtroller pushViewController:vc2 animated:TRUE completion:nil]
    

    if you aren't, adapt the code where your view controller is added to the window. If your VC is the rootWindowController and you are not using storyboarding, this is likely in your AppDelegate

    if you use storyboards, adapt the storyboard so you are inside a navigation controller


    ELSE if you don't want that for any reason: :) just manually animate in the 2. VC's view using [UIView animate:vc2.view ....]

    written inline -- method names don't match but shows general approach:

    UIView *v = vc2.view;
    CGRect f = v.frame;
    f.origin.x += self.view.frame.size.width; //move to right
    
    v.frame = f;
    
    [UIView animateWithDuration:0.5 animations:^{
        v.frame = self.view.frame;
    } completion:^(BOOL finished) {
       [self presentViewController:vc2 animated:NO completion:nil];
    }];
    

    in the completion block present the view controller vc2 non-animated as you already did that yourself