UIModalPresentationCurrentContext with Transition? - iphone

I am trying to modal present a view controller like below:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"addPopover"];
vc.view.backgroundColor = [UIColor clearColor];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:vc animated:YES];
Now using UIModalPresentationCurrentContext means I can present this view with a transparent background and see my other view behind the new one. However, it stops me from being able to present it with a transition.
Any ideas why? Or how I can get around this? Thanks.

I was able to accomplish this by setting modalPresentationStyle = UIModalPresentationCurrentContext on the rootViewController of my UIWindow, IF I haven't presented any new full screen viewControllers on top of this rootViewController. I did something like this:
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:YES completion:nil];
I've tried this with both a UINavigationController as my window's rootViewController and various other custom view controllers. It seems as long as the window's rootViewController knows what's going on, any sub-viewControllers can call presentViewController:animated:completion: without blacking-out the underlying view.
However, let's say you present another viewController on top of your window's rootViewController. And this new viewController is presented with modalPresentationStyle = UIModalPresentationFullScreen (ie. takes up the screen), then you have to call modalPresentationStyle = UIModalPresentationCurrentContext on that top-most viewController.
So to recap:
If you have UINavigationController -> UIViewController(s) (they could be pushed and popped in and out), then you set modalPresentationStyle = UIModalPresentationCurrentContext on the UINavigationController.
If you have UINavigationController -> UIViewController -> new-UIViewController (with modalPresentationStyle set to UIModalPresentationFullScreen), then you set modalPresentationStyle = UIModalPresentationCurrentContext on the new-UIViewController.
Hopefully this works for you guys as well!

Full screen modals aren't supposed to allow you to see the under layer. Annoying I know.
From your code I'm assuming that "addPopover" is actually a full screen view, where you have your content as a subsection while the rest is transparent.
Try this:
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:vc animated:YES completion:NULL];
Note: I'd recommend adding a low alpha background color to let the user know that they can't interact with the rest of the view when you pop this over top...

Mr. T's suggestions worked nearly perfectly, you just lose the transition animation on the presented viewcontroller
Since it is also setting the appDelegates rootviewController's presentation style, it will also remove the transition animations for any views presented from that point.
My fix was to return the AppDelegates rootViewController's presentation style back to it's default whenever the viewcontroller is closed that I needed the transparent background on
I have a button that dismisses the presentedViewController, in that I also set the presentation style of the rootViewController back to default using:
UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;
I hope that helps anyone else getting stumped on this problem.

Related

Translucent UINavigationController, presented modally

How can one present a UINavigationController such that the view it was presented from is still visible in the background?
My experience is that the UINavigationController will clip anything below its view, so setting UINavigationController.view.alpha will uncover a fixed color background, not the presenting view's content.
Can this be changed?
EDIT
I am not interested in the navigation bar, but the full content the navigation controller manages.
The problem is not the UINavigationController, but the fact that you present it modally. ViewControllers presented modally can never be transparent.
You can fake a modal presentation by adding you UINavigationControllers view as a subview to the main UIWindow.
This example works for me when testing in XCode:
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.35];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:viewController];
[[[[UIApplication sharedApplication] delegate] window] addSubview:navCon.view];
Of course you will have to do any animated transition yourself, but that should be trivial using animation blocks.
There is now a way to achieve this using the iOS7 custom transitions, this way :
MyController * controller = [MyController new];
[controller setTransitioningDelegate:self.transitionController];
controller.modalPresentationStyle = UIModalPresentationCustom;
[self controller animated:YES completion:nil];
To create your custom transition, you need 2 things :
A TransitionDelegate object (implementing
<UIViewControllerTransitionDelegate>)
An "AnimatedTransitioning" object
(implementing <UIViewControllerAnimatedTransitioning>)
You can find more informations on custom transitions in this tutorial : http://www.doubleencore.com/2013/09/ios-7-custom-transitions/
if you wish this behavior you can not use a UINavigationController, as you say the navigation controller clip the view of the content controller. To do what you want you should add a navigator bar to your view instead, and simulate the actions of the navigation controller. To create a back button similar to the controller read this article

Form Sheet Modal View Animations

I have UIModalPresentationFormSheet views appearing in my app. Some of them appear from the Right some from the Bottom and the dismissing seems random. Some disappear to the Bottom some go Left some go Up. Is there a way to set the direction they appear from and dismiss to?
Code I use to present (this same code, just different viewcontroller being presented, called from the same view controller has varying animations for different modal views):
MyViewController *newModalView = [[MyViewController alloc] init];
newModalView.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:newModalView animated:YES];
Then in the modal view I call this to dismiss it:
[self dismissModalViewControllerAnimated:YES];
This is a known bug and is being worked on by apple.
Try something like this:
newModalView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
All the transition Styles:
newModalView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
newModalView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
newModalView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

Presenting two nested modal view Controllers but show just the last transition

I have a 1st VC (view controller) that should be modal, and it has a child modal VC that sometimes should be presented as soon as 1st VC will appear, but sometimes not.
So from a root VC I want to present 1st VC, and over the 1st VC present the child modal VC. But the user should only see the child VC modal transition (not the 1st VC).
AFAIK the 1st VC can only present a modal VC after viewDidAppear:, so I don't know how to make this possible, since when viewDidAppear: is called, the 1st VC is already visible to the user.
Don't want the user to see 2 modal transitions one after the other, but just the last modal transition, the child's one.
Any hint?
I figured out the simplest solution to this if you still haven't found a suitable one. You can use a UINavigationController to hold the 2 nested view controllers you are trying to display modally.
In the function that is meant to display the modal views you could do something like:
- (IBAction)showView3
{
ViewController2 *new2 = [[ViewController2 alloc] init];
ViewController3 *new3 = [[ViewController3 alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:new2];
nav.navigationBarHidden = YES;
[nav pushViewController:new3 animated:NO];
[self presentModalViewController:nav animated:YES];
}
Then function in ViewController3 to dismiss it would have:
[self.navigationController popViewControllerAnimated:YES];
And the one in ViewController2 would have:
[self dismissModalViewControllerAnimated:YES];
The only issue I can see with this is aesthetics, as by default the transition from view3 to view2 is a horizontal animation but the one from view2 back to view1 is vertical. You could of course change that as well to make them all horizontal, or all vertical, or however you want.
You could have 1 modal view controller with 2 views. Then just pick which view you want to display when the view controller loads.
You should be able to put presentModalViewController anywhere you want, including viewWillAppear.
[self presentModalViewController:myModalViewController animated:NO];
edit: for anyone reading this, see my other (correct) answer, because this one isn't.

presentModalViewController NOT animating when showing a TTMessageController

I have a subclass of TTMessageController that shows ... BUT it is not animated even though it should be. The code that displays the modal view looks like this (where PostToWebMessageController is the subclass of TTMessageController:
if (self.toWebMsgController == nil) {
self.toWebMsgController = [[PostToWebMessageController alloc] init];
}
UINavigationController *navController = [[UINavigationController alloc] init];
[navController pushViewController:self.toWebMsgController animated:NO];
[self presentModalViewController:navController animated:YES];
What happens though is this: The screen goes black ... the keyboard scrolls up into view ... and THEN the TTMessageController view shows up (not animated). When I dismiss the view via a Cancel button the screen goes black and then just disappears (no animation again).
Any ideas why this is happening? I've this with a number of other TT* controllers and I can't get one to animate right with showing modally.
Thanks
UPDATE:
This is happening in EVERY UIViewController that I try to present modally. Screen goes black, keyboard animates upwards and then view displays. Any ideas why this might be happening???
A day to figure this out ... hopefully someone will benefit from my pains!
Here is what is happening:
The UIViewController calling presentModalViewController is itself nested inside a UIScrollView that is contained in ANOTHER UIViewController. Apparently, cocoa touch doesn't much like this. Anyhow, to rectify the problem I did the following:
Add a property of type UIViewController to the UIViewController that will present a modal view controller (e.g. #property (nonatomic, retain) UIViewController *owningController;)
Set that property = to the topmost UIViewController (the one that contains the UIScrollView in this case)
In the UIViewController that shows the modal view ... change this
[self presentModalViewController:controller animated:YES];
to this ...
[owningController presentModalViewController:controller animated:YES];
I'm not sure why you are using a UINavigationController. If it is because you would like your toWebMsgController controller to have a nav bar when it loads in the modal view, try the following alterations to your code:
if (self.toWebMsgController == nil) {
self.toWebMsgController = [[PostToWebMessageController alloc] init];
}
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:toWebMsgController];
//[navController pushViewController:self.toWebMsgController animated:NO];
[self presentModalViewController:navController animated:YES];
If you don't require a nav bar in your modal view, you probably don't need a UINavigationController at all.
I had same issue.
Check that you root controller (if you present controller over it) for presentationStyle DOES NOT set to UIModalPresentationCurrentContext

presentmodalviewcontroller navigationcontroller

I am creating a Navigation based iPhone application.
In that I have called a UiViewController using presentModalViewController. After that, the ViewController becomes visible. From that ViewController I need to call another ViewController using the sample presentModalViewController. Is this possible or not?
What do you mean by "call another uiviewcontroller"? (It really helps if you can be more detailed in your question.) If you mean, "slide in another view controller", then:
MyNewViewController *myNewViewController = [[MyNewViewController alloc] initWithNibName:#"MyNewViewController" bundle:nil];
[navigationController pushViewController:myNewViewController animated:YES];
[myNewViewController release];
...where:
MyNewViewController is the new view controller class that you want to slide in (the above code assumes you have an XIB file for the view controller class).
navigationController points to the current navigation controller. You'll have to replace it with something like [self navigationController], depending where you are in the view hierarchy.
U might be using following line to present a view controller.
//assume name of viewController which u want to present is "myViewController"
[self.navigationController presentModalViewController:myViewController animated:YES]
If you want to push an other ViewController or present an other ViewController then u will need to replace above line with following lines.
//[self.navigationController presentModalViewController:myViewController animated:YES];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navigationController.navigationBarHidden = YES; //if u want to show navigation bar then remove this line
[self presentModalViewController:navigationController animated:YES];
After using above code you can present or push other view controllers within presented view controller.
Hope it will solve your problem :)