添加关闭按钮到UIModalPresentationPageSheet角落

时间:2011-08-08 00:34:14

标签: ios objective-c modal-dialog

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

有解决方案吗?

感谢。

3 个答案:

答案 0 :(得分:1)

这是我使用的解决方案。它不是你所描述的,它也会很整洁,但是因为你希望按钮部分地超出视图范围(因为你说它必须是视图控制器的一个孩子),所以会很棘手查看超级视图)。

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

答案 1 :(得分:0)

编辑:实际上,我建议你做的第一件事是使用另一种关闭按钮。例如,您可以使用Done按钮在顶部添加工具栏。

如果你仍然想要Apple风格的浮动X,那么尝试设置属性以确保它不会被隐藏或剪裁。我想说更好的方法是创建一个UIButton,前景图像是样式化的按钮图像,背景图像从页面背景颜色/图案渐变到透明背景按钮周围。它有效地为您提供了“浮动关闭按钮”,而无需超出页面范围。

答案 2 :(得分:0)

我发现@ TomSwift的建议很有帮助。这是一个使用未弃用的iOS7方法和ARC的版本。

@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];
        [nc setToolbarHidden:YES animated: NO];
        nc.modalPresentationStyle = modalViewController.modalPresentationStyle;

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

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

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

    [self presentViewController:nc animated:animated completion:nil];
}

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

- (void)autoModalViewControllerDismiss: (id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

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

@end

我称之为......

MyController *vc = [[MyController alloc] init];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;   
[self presentAutoModalViewController:info animated:YES];