BlueBear
BlueBear

Reputation: 7639

iOS 7 present modal view inside UINavigationController?

My app is setup using a UINavigationController for all of the navigation. I am wanting to use a modal transition to present a specific view controller in the app. The thing is that I want to keep that new VC embedded inside of the navigation controller, but just that one view needs to use a modal animation rather than push.

How can I go about implementing this modal animation/transition on a single view controller within the UINavigationController?

Keep in mind that after this modal animation happens there will be buttons on that page that will proceed to work in line with a standard UINavigationController push animation.

Upvotes: 1

Views: 15277

Answers (6)

Leszek Szary
Leszek Szary

Reputation: 10346

Maybe something like this will work for you (self is UINavigationController):

- (void)verticalPushViewController:(UIViewController *)controller
{
    [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
    CATransition* transition = [CATransition animation];
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    transition.duration = 0.3;
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromTop;
    [self.view.layer addAnimation:transition forKey:kCATransition];
    [self pushViewController:controller animated:NO];
}

- (UIViewController *)verticalPopViewController
{
    [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
    CATransition* transition = [CATransition animation];
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    transition.duration = 0.3;
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromBottom;
    [self.view.layer addAnimation:transition forKey:kCATransition];
    return [self popViewControllerAnimated:NO];
}

Upvotes: 0

Mike Glukhov
Mike Glukhov

Reputation: 1830

The simplest way.

To present

LaunchImageViewController *launchView = [self.storyboard instantiateViewControllerWithIdentifier:@"launchImage"];
[[UIApplication sharedApplication].keyWindow addSubview:launchView.view];

To close. I calling this inside presented(LaunchImageViewController).

[self.view removeFromSuperview];

Upvotes: -1

Parvendra Singh
Parvendra Singh

Reputation: 985

Use this line not presentModelViewController

[self presentViewController:vc animated:YES completion:Nil];  

Upvotes: 0

Léo Natan
Léo Natan

Reputation: 57060

With iOS7, this is possible using the new custom transitioning API. You can find more information here: http://www.objc.io/issue-5/view-controller-transitions.html

You create an animation controller (interactive or otherwise), and pass it in the appropriate method of the navigation bar delegate. In your animation controller, you perform the custom animation using UIView animation API. In your case, the animation should perform a slide from bottom into place for forward animation, and slide from place to bottom for reverse animation. If you support interactive animation, you can even control the curve of the animation in relation to the finger slide.

Upvotes: 2

Kunal Balani
Kunal Balani

Reputation: 4789

Modal view by default is full screen. You need to change presentation style to show navigationBar

EDIT : This only works for iPad. For iPhone you can use MZformsheet or Kentnguyen to get semi modal behavior

MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease]; 

targetController.modalPresentationStyle = UIModalPresentationFormSheet;

targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; //transition shouldn't matter 

[self presentModalViewController:targetController animated:YES];


targetController.view.superview.frame = CGRectMake(0,44/*navigation bar's height*/,height, width);//it's important to do this after 
// use full screen height and width here

targetController.view.superview.center = self.view.center;

Upvotes: 1

Peter Foti
Peter Foti

Reputation: 5674

ITviewViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"vc"];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:Nil];

Replace the pseudo code with your view controller names.

Upvotes: 4

Related Questions