Jordan Smith
Jordan Smith

Reputation: 10378

Modal view controller won't dismiss itself

What I'm doing:

In my app, I'm presenting a modal view controller (containing app settings) using the following code:

    optionsViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    [self presentModalViewController:optionsViewController animated:YES];

This transition just curls up the bottom part of the view to expose a few settings. (See the 'Maps' app for an example.) When you tap on the top half of the page, where the original view is still there but grayed out, the modal view controller is automatically dismissed (handled by the OS, I didn't code for this).

-

What's not working:

This is working fine in iOS 4 (my app is currently on the App Store in fact). But in iOS 5, it looks like Apple have changed the behavior of this transition, and the view controller no longer dismisses itself. I'm trying to replicate the behavior that was handled by the OS before, but can't figure out how to.

-

What I've tried:

Adding an invisible button to the top of the options view doesn't work. The page then curls up the full way, which I don't want.

Apart from this, I'm stuck. How should I replicate how this worked originally (or was I doing it the wrong way from the start!). Any help is much appreciated!

Upvotes: 33

Views: 38245

Answers (6)

Linda Saul
Linda Saul

Reputation: 1

Thanks guys, this saved me a lot of time. I just noticed that the presentModalViewController and dismissModalViewController methods are deprecated according to the source code for UIViewControoler.h. There are alternative presentViewController and dismissViewController methods.

Upvotes: 0

Mazyod
Mazyod

Reputation: 22559

Dude, I ran into the same problem.. and here is what I found about using parentViewController:

Note that as of 5.0 this no longer will return the presenting view controller.

This was written in the header file of UIViewController...

I am using ShareKit, and the modalViewController was working perfectly in iOS4, but in iOS5, it just won't dismiss itself! This is because in their code, they are using:

    [[currentView parentViewController] dismissModalViewControllerAnimated:animated];

and parentViewController will return nil, since this is a modal presented view controller...

By searching for a solution, I found your question.. So, I decided to fix it myself :P

I changed the previous line to this:

    [currentView dismissModalViewControllerAnimated:YES];

Works like a charm.


EDIT: Depending on how you interpret the original question, there are two answers. Here's the second:

In iOS5 it seems that the modal controller only dismisses itself when you click the curl, but not above the curl or the backgound. In iOS5, in order to actually get the modal view to dismiss itself when tapping the background or above the curl I added the following code to the controller, to listen to taps on the modal view, but ignore taps to buttons. This should mimic the behavior in previous version of iOS when working with a modal controller with page curl.

- (void)viewDidLoad
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    tap.delegate = self;          
    [backgroundView addGestureRecognizer:tap];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    //change it to your condition    
    if ([touch.view isKindOfClass:[UIButton class]]) {      
       return NO;
    }
    return YES;
}

- (void)handleTap:(UITapGestureRecognizer *)sender {
    [self dismissModalViewControllerAnimated:YES];
}

Upvotes: 71

user468687
user468687

Reputation:

In iOS5 it seems that the modal controller only dismisses itself when you click the curl, but not above the curl or the backgound. In iOS5, in order to actually get the modal view to dismiss itself when tapping the background or above the curl I added the following code to the controller, to listen to taps on the modal view, but ignore taps to buttons. This should mimic the behavior in previous version of iOS when working with a modal controller with page curl.

- (void)viewDidLoad
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    tap.delegate = self;          
    [backgroundView addGestureRecognizer:tap];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    //change it to your condition    
    if ([touch.view isKindOfClass:[UIButton class]]) {      
       return NO;
    }
    return YES;
}

- (void)handleTap:(UITapGestureRecognizer *)sender {
    [self dismissModalViewControllerAnimated:YES];
}

Upvotes: 0

Luis Ascorbe
Luis Ascorbe

Reputation: 2099

I had the same problem, also affects those who use:

 [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

I fix it with a Observer, adding this where you had the dismiss:

[[NSNotificationCenter defaultCenter] postNotificationName:@"yourObserverName" object:self];

And this in the parent parent view controller:

// add in viewDidLoad for example
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissModalVCFromParent:) name:@"yourObserverName" object: nil];

//The function
- (void) dismissModalVCFromParent:(NSNotification *)notif
{
   [self dismissModalViewControllerAnimated:YES];
}

// Don't forget remove
[[NSNotificationCenter defaultCenter] removeObserver:self];

Upvotes: 1

benvolioT
benvolioT

Reputation: 4556

This seems to work on the (now final version of) ios 5.

I notice that you have to tap is a specific region to dismiss the page curl - tapping near the edges of the top portion of the screen does not seem to do anything, but the center, blurred section above the page curl graphic consistently results in dismissing the modal view.

I'm not sure whether that narrow tap region behavior is new to ios 5 or already existed and I never noticed before. Hopefully that is helpful!

Upvotes: 0

Ben Gottlieb
Ben Gottlieb

Reputation: 85522

What's the code you're using to dismiss the modal view controller? I've seen code like this:

[self.parentViewController dismissModalViewControllerAnimated: YES];

that doesn't work on all versions of the OS. However, this:

[self dismissModalViewControllerAnimated: YES];

should.

Upvotes: 15

Related Questions