Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add close button to UIModalPresentationPageSheet corner

Is there a way to add a button at the corner of an UIModalPresentationPageSheet? I mean, I want to put that Apple-like (x) button at the corner of a Page Sheet, but adding it to the parent view makes it appear behind the Page Sheet (and also impossible to tap) and adding it to the Page Sheet will make part of it hidden, since it's out of the view area.

Is there a solution?

Thanks.

like image 532
diegotrevisan Avatar asked Nov 13 '22 19:11

diegotrevisan


1 Answers

Here's a solution that I use. It's not quite what you describe, which would be neat too, but would be tricky since you'd want the button to be partially out of the view's bounds (as you say, it would have to be a child of the view-controller-view's superview).

My solution is to put a close button in the left-button area of a navigation bar. I do this automagically via a UIViewController class extension. To use it, just call [currentViewController presentAutoModalViewController: modalViewController animated: YES];

@implementation UIViewController (Modal)

- (void) presentAutoModalViewController: (UIViewController *) modalViewController withDismissAction: (SEL) onDismiss animated:(BOOL)animated
{
    UINavigationController* nc = nil;
    if ( NO == [ modalViewController isKindOfClass: [UINavigationController class]] )
    {
        nc = [[[UINavigationController alloc] initWithRootViewController: modalViewController] autorelease];

        [nc setToolbarHidden:YES animated: NO];

        nc.modalPresentationStyle = modalViewController.modalPresentationStyle;

        modalViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
                                                                                                              target:self 
                                                                                                              action:onDismiss] autorelease];
    }
    else
    {
        nc = (UINavigationController*) modalViewController;

        UIViewController* rootViewController = [nc.viewControllers objectAtIndex: 0];
        rootViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop
                                                                                                              target:self 
                                                                                                              action:onDismiss] autorelease];
    }

    [nc setNavigationBarHidden: NO];
    nc.navigationBar.barStyle = UIBarStyleBlack;
    nc.toolbar.barStyle = self.navigationController.navigationBar.barStyle;

    [self presentModalViewController: nc animated: animated ];
}

- (void) presentAutoModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
    [self presentAutoModalViewController:modalViewController withDismissAction: @selector(autoModalViewControllerDismiss:) animated: animated];
}

- (void) autoModalViewControllerDismiss: (id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL) isAutoModalViewController
{
    return ( self.navigationController != nil && self.navigationController.parentViewController != nil && self.navigationController.parentViewController.modalViewController == self.navigationController );
}

@end
like image 182
TomSwift Avatar answered Apr 26 '23 23:04

TomSwift