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 trial

iOS

Thomas Nilsen
Thomas Nilsen
14,957 Points

Animating 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
STAFF
Amit Bijlani
Treehouse Guest Teacher

This 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];
}
Thomas Nilsen
Thomas Nilsen
14,957 Points

I'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.

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Set the alpha transparency of the view property for your presented view controller to 0.

Thomas Nilsen
Thomas Nilsen
14,957 Points

Maybe 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
Amit Bijlani
Treehouse Guest Teacher

It won't hide the view it will just make it transparent. Try it out.

Thomas Nilsen
Thomas Nilsen
14,957 Points

I'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
Thomas Nilsen
14,957 Points

My 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
Amit Bijlani
Treehouse Guest Teacher

Sorry my bad. I meant to say background color property.

displayListVC.view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
Thomas Nilsen
Thomas Nilsen
14,957 Points

Nothing changes unfortunately...

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Since 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
Thomas Nilsen
14,957 Points

I 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
Amit Bijlani
Treehouse Guest Teacher

I 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
Thomas Nilsen
14,957 Points

I thought you were on to something there, but no. Same thing still happens - the view in the back disappears

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Provide me with the project files so I can take look.