11

In my code I am using presentViewController to call my second viewcontroller

[self presentViewController:secondController animated:YES completion:nil];

When I call I need to show left to right animation (like in navigationController)

I don't want to use the navigationController but I need the animation similar to navigationController in presentViewController...

swiftBoy
  • 33,793
  • 26
  • 129
  • 124
Subramanian Raj
  • 379
  • 2
  • 4
  • 16

5 Answers5

15

Add this line of code before presenting view controller

secondController.modalTransitionStyle   = UIModalTransitionStyleCrossDissolve;
secondController.modalPresentationStyle = UIModalPresentationFullScreen;

// Take a look at this enum

typedef enum {
   UIModalTransitionStyleCoverVertical = 0,
   UIModalTransitionStyleFlipHorizontal,
   UIModalTransitionStyleCrossDissolve,
   UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
Ch0k0l8
  • 807
  • 6
  • 12
  • No it did not worked, it simply goes without animation. secondController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; // Take a look at this enum typedef enum { UIModalTransitionStyleCoverVertical = 0, UIModalTransitionStyleFlipHorizontal, UIModalTransitionStyleCrossDissolve, UIModalTransitionStylePartialCurl, } UIModalTransitionStyle; [self presentViewController:secondController animated:YES completion:nil]; – Subramanian Raj Mar 29 '15 at 14:40
  • I gave like this but it did not worked secondController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;; secondController.modalPresentationStyle = UIModalPresentationFullScreen; ; // Take a look at this enum typedef enum { UIModalTransitionStyleCoverVertical = 0, UIModalTransitionStyleFlipHorizontal, UIModalTransitionStyleCrossDissolve, UIModalTransitionStylePartialCurl, } UIModalTransitionStyle; [self presentViewController:secondController animated:YES completion:nil]; – Subramanian Raj Mar 29 '15 at 14:49
  • @SubramanianRaj Do you set those properties before calling [UIViewController presentViewController:animated:completion]? – Ch0k0l8 Mar 29 '15 at 14:57
  • UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone5" bundle:nil]; HomeViewController *secondController = (HomeViewController *)[storyboard instantiateViewControllerWithIdentifier:@"test"]; secondController.LoginID= aID; secondController.LoginUsername= aUsername; secondController.UserProfileImage=ProfileImage; secondController.UserOtherInfo=UserInfo; – Subramanian Raj Mar 29 '15 at 15:34
  • @SubramanianRaj Well, change your presentation style to UIModalPresentationStyleOverCurrentContext – Ch0k0l8 Mar 29 '15 at 15:57
  • in iOS 11 only setting yourVC.modalTransitionStyle is working fine – Shehroz Sep 13 '18 at 11:22
6

for Swift

Define animations types


enum UIModalTransitionStyle : Int {
    case CoverVertical = 0
    case FlipHorizontal
    case CrossDissolve
    case PartialCurl
}

How to use


let vc : PageHomeViewController = storyboard!.instantiateViewControllerWithIdentifier("PageHomeViewController") as! PageHomeViewController
vc.modalTransitionStyle = .FlipHorizontal
self.presentViewController(vc, animated: true, completion: nil)
swiftBoy
  • 33,793
  • 26
  • 129
  • 124
3

My decision for resolving the animation "cover horizontal" like a UINavigationViewController push method with using UIViewControllerTransitioningDelegate.

1.Create a custom transition.

Header

@interface CoverHorizontalTransition: NSObject<UIViewControllerAnimatedTransitioning>
@property (assign, nonatomic) BOOL dismiss;
@end

Implementation

@implementation CoverHorizontalTransition

- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController;
    fromViewController =
    [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    UIViewController *toViewController;
    toViewController =
    [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UIView *containerView = transitionContext.containerView;

    CGRect animatedViewFrame;
    animatedViewFrame = containerView.bounds;
    animatedViewFrame.origin = CGPointMake(CGRectGetWidth(animatedViewFrame), 0);

    [containerView addSubview:toViewController.view];

    if (_dismiss) {
        [containerView bringSubviewToFront:fromViewController.view];

        [UIView
         animateWithDuration:[self transitionDuration:transitionContext]
         animations:^{
             fromViewController.view.frame = animatedViewFrame;
         } completion:^(BOOL finished) {
             [containerView.superview addSubview:toViewController.view];
             [fromViewController.view removeFromSuperview];
             [transitionContext completeTransition:YES];
         }];
    } else {
        toViewController.view.frame = animatedViewFrame;

        [UIView
         animateWithDuration:[self transitionDuration:transitionContext]
         animations:^{
             toViewController.view.center = containerView.center;
         } completion:^(BOOL finished) {
             [transitionContext completeTransition:YES];
         }];
    }
}

- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.25;
}

@end

2.Create transition delegate.

@implementation CustomViewControllerTransitioningDelegate

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return [CoverHorizontalTransition new];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    CoverHorizontalTransition *transition;
    transition = [CoverHorizontalTransition new];
    transition.dismiss = YES;

    return transition;
}

@end

Sample of using.

...
// Save delegate to strong property
secondController.customTransitioningDelegate =
[BaseViewControllerTransitioningDelegate new];

secondController.transitioningDelegate =
secondController.customTransitioningDelegate;
secondController.modalPresentationStyle = UIModalPresentationCustom;

[self presentViewController:secondController animated:YES completion:nil];

This code works for iOS 10+.

  • In my opinion the best solution. Works just like a nav controller transition. Thanks. – Guy Lowe Apr 15 '19 at 08:22
  • According to Apple's documentation at https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/CustomizingtheTransitionAnimations.html#//apple_ref/doc/uid/TP40007457-CH16-SW15, you should be using viewForKey to get the view to be added or removed instead of the view controllers' view property. – Arda Aug 25 '19 at 19:29
0

All of the above can also be achieved in storyboard without having to write code. Click on your segue that links your view controllers, then select the Attributes Inspector tab in the right side menu. In the Kind drop down menu select 'Present Modally'. Another set of options will come up. In the Transition drop down menu you will be able to select any of the enums listed above. Segue Attributes Inspector

elarcoiris
  • 1,515
  • 3
  • 22
  • 24
-1

@swiftboy answer is most correct. You don't need to declare the enum and you can call it directly.

let vc : PageHomeViewController = 
storyboard!.instantiateViewControllerWithIdentifier("PageHomeViewController") 
as! PageHomeViewController
vc.modalTransitionStyle = .FlipHorizontal 
self.presentViewController(vc, animated: true, completion: nil)
noamt
  • 6,006
  • 2
  • 33
  • 50
iOSArchitect.com
  • 4,550
  • 1
  • 13
  • 34
  • This is just a copy of another answer to this question. Please only add an answer if you have something new to add. – FluffyKitten Sep 25 '17 at 08:01