【问题标题】:Add close button to UIModalPresentationPageSheet corner将关闭按钮添加到 UIModalPresentationPageSheet 角落
【发布时间】:2011-08-08 00:34:14
【问题描述】:

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

有解决办法吗?

谢谢。

【问题讨论】:

  • 您所说的“将其添加到页面表将使其一部分隐藏,因为它不在视图区域内。”是什么意思?

标签: ios objective-c modal-dialog


【解决方案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

【讨论】:

    【解决方案2】:

    编辑:实际上,我建议您做的第一件事是使用不同类型的关闭按钮。例如,您可以在顶部添加一个带有Done 按钮的工具栏。

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

    【讨论】:

    • clipsToBounds 对页面表内显示的视图没有影响,因为它不能超出页面表的边界。
    【解决方案3】:

    我发现@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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多