presentViewController animation from side
Asked Answered
A

2

8

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.

Acheron answered 24/2, 2013 at 13:42 Comment(0)
W
9

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

Wollis answered 24/2, 2013 at 14:10 Comment(8)
a navigation controller doesn't need to show a navigation bar btw :)Wollis
Now I call the vc2 with the following: AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; CreateEntryViewController *vc2 = [[CreateEntryViewController alloc] init]; [appDelegate.navController pushViewController:vc2 animated:TRUE]; But I cannot return with what I thought would be correct: AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; [appDelegate.navController dismissViewControllerAnimated:TRUE completion:nil]; Any thoughts?Acheron
yes, you don't want to call the vc2 with the appDelegate -- thats.... weird -- call it using your OWN navigation controller. [the one that contains vc1] (vc1.navigationController)Wollis
I just found out that I in a ViewController simply can call [self.navigationController dismissVi...] - but it still doesn't work - there is no action when I press my own button. The reason I need this is that I have disabled the nav bar :)Acheron
you need a delegate for this -- see the apple app: utility app template and how the two VCs communicate there :)Wollis
Thanks for the help. Where can I find that app?Acheron
CGRect f = f.frame; what is it?Gauffer
it should be f = v.frame; - typoWollis
P
0

This helped me,

- (void)presentNewViewController{
    NewViewController *objNewViewController =[[NewViewController alloc]initWithNibName:@"NewViewController" bundle:nil];

    UIView *tempNewVCView = [UIView new];
    tempNewVCView = objNewViewController.view;
    tempNewVCView.frame = self.view.frame;

    CGRect initialFrame = self.view.frame;
    initialFrame.origin.x = self.view.frame.size.width;

    tempNewVCView.frame = initialFrame;

    [self.view addSubview:tempNewVCView];

    [UIView animateWithDuration:0.3 animations:^{
        tempNewVCView.frame = self.view.frame;
    } completion:^(BOOL finished) {
        [self presentViewController:objNewViewController animated:NO completion:^{
        }];
    }];
}
Pucker answered 29/9, 2016 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.