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.

casparjespersen
  • 3,460
  • 5
  • 38
  • 63

2 Answers2

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

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • 1
    a navigation controller doesn't need to show a navigation bar btw :) – Daij-Djan Feb 24 '13 at 14:13
  • 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? – casparjespersen Feb 24 '13 at 17:09
  • 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) – Daij-Djan Feb 24 '13 at 17:13
  • 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 :) – casparjespersen Feb 24 '13 at 17:32
  • you need a delegate for this -- see the apple app: utility app template and how the two VCs communicate there :) – Daij-Djan Feb 24 '13 at 17:39
  • Thanks for the help. Where can I find that app? – casparjespersen Feb 24 '13 at 17:48
  • CGRect f = f.frame; what is it? – Chanchal Raj Sep 28 '15 at 11:11
  • it should be f = v.frame; - typo – Daij-Djan Sep 28 '15 at 11:47
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:^{
        }];
    }];
}
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130