如何在UIModalPresentationPageSheet中显示的模态视角添加关闭按钮? [英] how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?

查看:1071
本文介绍了如何在UIModalPresentationPageSheet中显示的模态视角添加关闭按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UIModalPresentationPageSheet 视图的角落添加一个浮动关闭(x)按钮。效果如下:

I want to add a floating close (x) button at the corner of an UIModalPresentationPageSheet View. The effect is like below:

但是将它添加到父视图会使它显示在页面页面后面(也无法点击)并将其添加到页面表中会使其中的一部分隐藏,因为它不在视图区域。

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 any better solution?

任何建议都表示赞赏。

Any suggestions are appreciated.

推荐答案

您可以尝试将其添加到最顶层的应用程序窗口:

You can try and add it to the the topmost application window:

    add.modalPresentationStyle = UIModalPresentationPageSheet;
    [self presentViewController:add animated:YES completion:^{
        [[[[UIApplication sharedApplication] windows] lastObject] addSubview:button];
    }];

这应该为您提供正确的视图,让按钮显示在模态顶部并接收触摸事件。

This should give you the right view to allow the button to appear on top of the modal and receive touch events.

- 编辑 -

要定位按钮,你可以尝试这样的事情:

To position the button you can try something like this:

self.modalPresentationStyle = UIModalPresentationFormSheet;
add.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:pvc animated:YES completion:^{
    CGRect parentView = pvc.view.superview.frame;
    CGRect buttonFrame = button.frame;
    buttonFrame.origin = CGPointMake(parentView.origin.x - buttonFrame.size.width/2.0f, parentView.origin.y - buttonFrame.size.height/2.0f);

    [button setFrame:buttonFrame];
    [[[[UIApplication sharedApplication] windows] lastObject] addSubview:button];
}];

这将获得已呈现的modalView的帧。然后,您可以将按钮的原点设置为偏移并设置调整后的帧。

This will get the frame of the modalView that has been presented. You can then set your button's origin to be offset and set the adjusted frame.

这篇关于如何在UIModalPresentationPageSheet中显示的模态视角添加关闭按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆