iOS 7 present modal view inside UINavigationController? - ios

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.

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.

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.

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;

Use this line not presentModelViewController
[self presentViewController:vc animated:YES completion:Nil];

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];
}

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];

Related

dismissViewControllerAnimated not animating ViewController

I want to slide in a ViewController in the screen, and then at some point slide it out and go back to the presenting ViewController.
I use this code from my MainViewController to launch the modal, and then to dismiss it.
Why is the dismiss slide animation not working?
// Present a React Component sliding from the right on top of current ViewController
+ (void)presentSlidingFromRight:(UIViewController*)presented onViewController:(UIViewController*)presenterVC {
CATransition *transition = [[CATransition alloc] init];
transition.duration = 1;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[presenterVC.view.window.layer addAnimation:transition forKey:kCATransition];
[presenterVC presentViewController:presented animated:NO completion:nil];
}
// Dismiss a React Component sliding to the right
+ (void)dismissSlidingToRight:(UIViewController*)presented onViewController:(UIViewController*)presenterVC {
CATransition *transition = [[CATransition alloc] init];
transition.duration = 3;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[presenterVC.view.window.layer addAnimation:transition forKey:kCATransition];
[presenterVC dismissViewControllerAnimated:NO completion:nil];
}
Could you try changing this line:
[presenterVC dismissViewControllerAnimated:NO completion:nil];
for this one:
[presenterVC dismissViewControllerAnimated:YES completion:nil];
Update
Make sure you are calling your static functions on the Main Thread

Using CATransition to custom viewcontroller transition animation

I have two viewcontroller A and B. A support portrait and landscape, B only support portrait.The below codes are on A.
UIStoryboard *board = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
B *vcb = [board instantiateViewControllerWithIdentifier:#"B"];
CATransition *a=[CATransition animation];
a.duration = 0.4f;
a.type = kCATransitionMoveIn;
a.subtype = kCATransitionFromTop;
a.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[self.view.window.layer addAnimation:a forKey:#"kTransitionAnimation"];
[self presentViewController:vcb animated:NO completion:nil];
And on B ,i do the dissmiss.
CATransition *a=[CATransition animation];
a.duration = 0.4f;
a.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
a.type = kCATransitionReveal;
a.subtype = kCATransitionFromBottom;
[self.view.window.layer addAnimation:a forKey:#"dismiss"];
[self dismissViewControllerAnimated:NO completion:nil];
It works ok on portrait mode. But it works wrong when i do the bellow steps:
1. Present the B on portrait mode
2. Turn the cellphone to landscape and then click to dissmiss B.
3. The animation was wrong.
Can anybody explain this and how to fix it.I want the B act same as on the portrait mode.
Not sure, I understand your question correctly. Probably you should handle the DeviceOrientation in your Controller A.

transition causes views to change from black to clear

I am using the following code to pop a navigation controller from one view controller to a second.
-(void)viewProfile :(UIButton *)sender{
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
ProfileViewController *newVC = [[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil];
[allViewControllers insertObject:newVC atIndex:0];
self.navigationController.viewControllers = allViewControllers;
CATransition* transition = [CATransition animation];
transition.duration = 0.2f;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[self.navigationController popViewControllerAnimated:NO];
}
The code works quite well. The one flaw is on the transition the view controller coming or ProfileViewController appears black. Then over the course of the animation it brightens to the default value, meanwhile the view controller calling this function becomes black.
How do I prevent this shading and just have the views always have their default colour throughout the transition?

wMemory Warning and application Crash due to poor ViewController handling?

I have a line of viewControllers set in line. The idea is to go through a process of questions etc. If i was to use the already set up way of changing view controller (through button - modal), i would have no problem with memory. I have tried it. However i need to change the views programmatically and not with buttons directly.
When i do this programmatically my memory usage just starts increasing as i go through the views, until eventually i get a Memory Warning and a crash.
To change the viewController i use custom UIStoryBoardSegue and also methods in the viewController files.
Here is how i do it:
Method in View Controller:
-(void)changeViewController
{
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:transition forKey:nil];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"enterInfoSecondViewController"];
[[[[[UIApplication sharedApplication]delegate]window]rootViewController]presentViewController:vc animated:NO completion:nil];
// [self dismissViewControllerAnimated:YES completion:nil];
//[self presentViewController:vc animated:NO completion:nil];
// [self dismissViewControllerAnimated:NO completion:nil];
}
In custom Segue:
-(void)perform
{
UIViewController *srcViewController = (UIViewController *)self.sourceViewController;
UIViewController*destViewController = (UIViewController *)self.destinationViewController;
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[srcViewController.view.window.layer addAnimation:transition forKey:nil];
[srcViewController presentViewController:destViewController animated:NO completion:nil];
}
......
Somewhere I do it with segue and somewhere with the top method.
What am i doing wrong? i need help please :) Thank you in advance.
If all you need is to trigger the segue programmatically,
why not use -performSegueWithIdentifier:sender: ?

Can I set maximum alpha for CATransition?

Is it possible to adjust the maximum alpha (opacity) in a CATransition for when the views fade in and out?
What I want is something that looks like a modal segue. Compared to a default modal segue, the transition given by CATransition is very «dramatic».
Say I have two views.
A: the view I want to transition from.
B: the view I want to transition to.
I want B to come moving in from the bottom over A. For this I use the kCATransitionMoveIn type, with subtype kCATransitionFromTop (which is weird because B is moving up from bottom, not top).
In the default modal segue, this works fine, and A is only greyed out a little. With CATransition, A is totally black at towards the end of the transition when B has moved about 70% over A.
Code:
UIStoryboard *loginBoard = [UIStoryboard storyboardWithName:#"Login" bundle:nil];
UIViewController *vc = [loginBoard instantiateInitialViewController];
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
[keyWindow insertSubview:vc.view belowSubview:keyWindow.rootViewController.view];
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromTop;
[keyWindow.layer addAnimation:transition forKey:kCATransition];
[keyWindow.rootViewController.view removeFromSuperview];
keyWindow.rootViewController = vc;
The problem originates from here.
After digging around some more I figured that you simply can't set a maximum opacity / alpha for the CATransition that you get out of the box.
So I solved it like this:
//offset the frame
vc.view.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
[UIView animateWithDuration:5
animations:^{
//insert over/before root view instead of after
[keyWindow insertSubview:vc.view aboveSubview:keyWindow.rootViewController.view];
vc.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
keyWindow.rootViewController.view.alpha = 0.8;
}
completion:^(BOOL finished) {
NSLog(#"completed! now you can change the rootviewcontroller etc..");
}];

Resources