简体   繁体   English

如何在UIModalPresentationPageSheet中显示的模态视图角添加关闭按钮?

[英]how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?

I want to add a floating close (x) button at the corner of an UIModalPresentationPageSheet View. 我想在UIModalPresentationPageSheet视图的一角添加一个浮动关闭(x)按钮。 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. 这应该给您正确的视图,以允许按钮出现在模式顶部并接收触摸事件。

--edit-- - 编辑 -

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];
}];

This will get the frame of the modalView that has been presented. 这将获取已呈现的modalView的框架。 You can then set your button's origin to be offset and set the adjusted frame. 然后,您可以将按钮的原点设置为偏移并设置调整后的框架。

See if this works: 查看是否可行:

myPresentedView.clipsToBounds = NO; myPresentedView.clipsToBounds = NO;

That will allow the button to draw itself beyond it's views. 这将使按钮能够将自身绘制到其视图之外。 One drawback to this is that the touches will not land beyond the views bounds, so try not to set the button too far away. 这样的一个缺点是触摸不会落在视图范围之外,因此请尽量不要将按钮设置得太远。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加关闭按钮到UIModalPresentationPageSheet角落 - Add close button to UIModalPresentationPageSheet corner 如何使用角落上方的关闭按钮创建视图? - How to create view with close button above the corner? 导航按钮问题中呈现的模态视图 - Modal view presented in navigation button issue 如何正确关闭模态控制器 - How to correctly close modal presented controller 如何有条件地向将在 NavigationView 中显示的 SwiftUI View 添加尾随的 navigationbaritem? - How to add a trailing navigationbaritem conditionally to a SwiftUI View which will be presented in a NavigationView? 如何知道视图控制器是作为弹出窗口还是模态显示? - How to know if a view controller will be presented as a popover or modal? iOS提供了一个模态视图控制器,之前已经介绍过 - iOS present a modal view controller which is presented before 如何在uinavcontroller中添加一个显示为模态的视图 - how to add a view in uinavcontroller which is displaying as a modal 如果存在多个视图控制器,如何关闭模态视图控制器 - How to dismiss modal view controller if there are multiple view controller are presented 如何为模态视图表单实现“ X”关闭按钮 - How to implement “X” close button for Modal View Form Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM