[英]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.