添加关闭按钮到UIModalPresentationPageSheet角落

die*_*san 5 modal-dialog objective-c ios

有没有办法在UIModalPresentationPageSheet的角落添加按钮?我的意思是,我想在页面的一角放置类似Apple的(x)按钮,但是将其添加到父视图会使它显示在页面页面后面(也无法点击)并将其添加到页面工作表会将其中的一部分隐藏起来,因为它不在视图区域内.

有解决方案吗?

谢谢.

Tom*_*ift 1

这是我使用的解决方案。这并不完全是你所描述的那样,这也很简洁,但是会很棘手,因为你希望按钮部分超出视图的边界(正如你所说,它必须是视图控制器的子级)视图的超级视图)。

我的解决方案是在导航栏的左侧按钮区域放置一个关闭按钮。我通过 UIViewController 类扩展自动执行此操作。要使用它,只需调用 [currentViewControllerpresentAutoModalViewController: modalViewControlleranimated: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
Run Code Online (Sandbox Code Playgroud)