1

Here's my current screen:

My Bookings screen

This screen consists of a UIViewController with programatically created buttons Current Bookings and Old Bookings. I have the logic on how to make the tapped active by adding a UIView as a subview to the UIButton.

All the space below the buttons is occupied by a container view where I want to add different UIViewControllers based on which button is active.

Initially, I do this to load one of the view controllers when the parent view appears,

DRMCurrentBookingsViewController *cbvc = [self.storyboard instantiateViewControllerWithIdentifier:@"CurrentBookingsController"];
[self addChildViewController:cbvc];
// 64 is the height of the navigation bar
cbvc.view.frame = CGRectMake(0, 64+50, self.view.bounds.size.width, self.view.bounds.size.height-50);
[container addSubview:cbvc.view];
[cbvc didMoveToParentViewController:self];

Then, when the user taps on a different button, I use this code to switch the views.

DRMCurrentBookingsViewController *newController = [self.storyboard instantiateViewControllerWithIdentifier:@"CurrentBookingsController"];
DRMOldBookingsViewController *oldController = [self.childViewControllers objectAtIndex:0];

        [oldController willMoveToParentViewController:nil];
        [self addChildViewController:newController];

        [self transitionFromViewController:oldController
                          toViewController:newController
                                  duration:0.5
                                   options:UIViewAnimationOptionTransitionNone
                                animations:^{
                                    // no further animations required
                                    newController.view.frame = oldController.view.frame;
                                }
                                completion:^(BOOL finished) {
                                    [oldController removeFromParentViewController]; 
                                    [newController didMoveToParentViewController:self];
                                }];

But, I want the child views to slide from left-right or right-left depending on which button is tapped and I do not want to select the pre-defined UIViewAnimationOption. I do not even wish to fade in the views. The views have to slide from one end to the other.

How can I achieve this?

Thanks for any help.

Suyash Shetty
  • 513
  • 3
  • 8
  • 17
  • you want push viewController or add subView, if you want add viewController it will not show above two buttons so I think you will add subview as another viewController view – Pravin Tate Jul 14 '15 at 04:43
  • in which direction Do you want to move child view ? – Anoop Jul 14 '15 at 05:47

2 Answers2

1

Got it. This is how I did it:

DRMCurrentBookingsTableViewController *newController = [self.storyboard instantiateViewControllerWithIdentifier:@"CurrentBookingsController"];
    DRMOldBookingsTableViewController *oldController = [self.childViewControllers objectAtIndex:0];

    [oldController willMoveToParentViewController:nil];
    [self addChildViewController:newController];

    // I added these
    CGRect newFrame = container.frame;
    newFrame.origin.x -= self.view.bounds.size.width;
    newFrame.size.width = self.view.bounds.size.width;
    newController.view.frame = newFrame;
    newFrame = container.frame;
    newFrame.size.width = self.view.bounds.size.width;

    [self transitionFromViewController:oldController
                      toViewController:newController
                              duration:0.25
                               options:UIViewAnimationOptionCurveEaseInOut
                            animations:^{
                                // no further animations required
                                newController.view.frame = newFrame;
                            }
                            completion:^(BOOL finished) {
                                [oldController removeFromParentViewController];
                                [newController didMoveToParentViewController:self];
                            }];

Thanks for all the help.

Suyash Shetty
  • 513
  • 3
  • 8
  • 17
0

I don't think "left to right" and "right to left" are available as an apple build in animations. I think you should use 2 container view and use [UIView animationWithDuration: method to create the "left to right" animation

ozd
  • 1,194
  • 10
  • 34