Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialThomas Nilsen
14,957 PointsAnimating viewController modally
I have a ViewController that I'm displaying with modalTransitionStyle = UIModalPresentationCustom. This works fine. But when the animation finishes and the viewController is displayed, the viewController in the background disappears. I don't understand why. I included a video of my problem - http://videobam.com/DQTSG. Let me know if I should include code as well.
5 Answers
Amit Bijlani
Treehouse Guest TeacherThis was really bugging me so and I finally found the solution. The were two key missing pieces. One is setting the presenting view controller's modalPresentationStyle
to UIModalPresentationCurrentContext
and also setting the modalPresentationStyle
of the presented view controller to: UIModalPresentationCustom
.
This is what the method looks like and it works:
- (void)showTableView:(id)sender
{
self.modalPresentationStyle = UIModalPresentationCurrentContext;
DisplayListViewController *displayListVC = [[DisplayListViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:displayListVC];
displayListVC.venues = self.venues;
displayListVC.userLocation = self.mapView.userLocation.location;
navController.modalPresentationStyle = UIModalPresentationCustom;
navController.transitioningDelegate = self;
[self presentViewController:navController animated:YES completion:nil];
}
Amit Bijlani
Treehouse Guest TeacherSet the alpha transparency of the view
property for your presented view controller to 0.
Thomas Nilsen
14,957 PointsMaybe I'm misunderstanding, but won't alpha = 0, hide the entire view? When you watch the video you see the viewController get's displayed modally on top of the map, and seconds after the animation completes, the map in the background disappears. I wan't to avoid that (meaning keep the map visible)
Amit Bijlani
Treehouse Guest TeacherIt won't hide the view it will just make it transparent. Try it out.
Thomas Nilsen
14,957 PointsI'd apreciate it if you could delete my last post with the dropbox link - I don't need everybody checking it out. Thanks again for taking a look :)
Thomas Nilsen
14,957 PointsMy MapViewController looks like this now:
- (void)showTableView:(id)sender
{
DisplayListViewController *displayListVC = [[DisplayListViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:displayListVC];
displayListVC.venues = self.venues;
displayListVC.userLocation = self.mapView.userLocation.location;
[displayListVC.view setAlpha:0]; //This is the line I added
navController.modalTransitionStyle = UIModalPresentationCustom;
navController.transitioningDelegate = self;
[self presentViewController:navController animated:YES completion:nil];
}
this hide the view that gets displayed completely
Amit Bijlani
Treehouse Guest TeacherSorry my bad. I meant to say background color property.
displayListVC.view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
Thomas Nilsen
14,957 PointsNothing changes unfortunately...
Amit Bijlani
Treehouse Guest TeacherSince you presenting that modally you don't need a navigation controller. Try removing that and presenting displayListVC directly.
- (void)showTableView:(id)sender
{
DisplayListViewController *displayListVC = [[DisplayListViewController alloc] init];
displayListVC.venues = self.venues;
displayListVC.userLocation = self.mapView.userLocation.location;
displayListVC.view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
displayListVC.modalTransitionStyle = UIModalPresentationCustom;
displayListVC.transitioningDelegate = self;
[self presentViewController:displayListVC animated:YES completion:nil];
}
Thomas Nilsen
14,957 PointsI have also tried that. The only reason I embedded it in a navigationController was to have a way to dismiss the view since I was displaying a tableViewController. I can zip the project and provide you have a dropbox link if you'd like.
Amit Bijlani
Treehouse Guest TeacherI think the problem could be that you are using a UITableViewController
. Maybe if you used a regular UIViewController
and then added in a UITableView
that would help.
Thomas Nilsen
14,957 PointsI thought you were on to something there, but no. Same thing still happens - the view in the back disappears
Amit Bijlani
Treehouse Guest TeacherProvide me with the project files so I can take look.
Thomas Nilsen
14,957 PointsThomas Nilsen
14,957 PointsI'm about to cry/laugh :P So tiny mistake... Anyways - Thank you so much Amit!! Btw - I tried without your first line, and that also worked actually.