ios - 如何在 iOS 中显示小型模态视图控制器?
问题描述
我想知道如何显示一个小视图控制器,它以模态方式占据视图整体高度的一小部分。
我创建了一个TestViewController
类,其中包含一个UIButton
我关心的单曲。此视图控制器在前一个视图控制器中执行某个操作后显示,并且仅占总高度的一小部分。
到目前为止,我有这段代码,它实现了使视图控制器“更小”并占据特定大小的目标。
TestViewController *testViewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
CGRect frame = CGRectMake(0, self.view.frame.size.height-80.0, self.view.frame.size.width, 80.0);
testViewController.view.frame = frame;
[self addChildViewController:testViewController];
[self.view addSubview:testViewController.view];
[testViewController didMoveToParentViewController:self];
但是,我如何配置这个视图控制器,以便如果我在视图控制器之外点击,我要么得到通知并可以采取行动(即解雇我自己),要么完全阻止触摸?这可能吗?
我尝试让视图控制器填满整个屏幕,然后有一个透明视图用于“拦截”发生在用于显示我的内容的小子视图之外的触摸,但我无法让它UIViewController
变得透明,使用清晰的背景会导致显示黑色背景。
我目前正在查看该modalPresentationStyle
物业,但到目前为止似乎没有帮助。
解决方案
For making UIViewController
transparent you have to present ModalViewController
and set property of modalPresentationStyle
to UIModalPresentationOverCurrentContext
.
ModalViewController *mvc = [[ModalViewController alloc]initWithNibName:@"ModalViewController" bundle:[NSBundle mainBundle]];
mvc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:mvc animated:YES completion:nil];
In ModalViewController
you need to set backgroundcolor
clear and set opaque
false.
self.view.backgroundColor = [UIColor clearColor];
self.view.opaque = false;
If you want to make transparent your ModalViewController
then set backgroundcolor
and opacity as per your requirement.
推荐阅读
- docker - 在 Docker 中的挂载文件夹中访问
- c# - 通过 rotativa 在本地获取 PDF 时,为什么我不能通过在线下载 PDF?
- gluon - 有没有办法使用胶子地图进行导航或方向?
- c# - 更改脚本的一个属性值,使其影响该脚本附加到的所有游戏对象
- excel - 从 Excel 通过 Outlook 发送邮件
- spring-boot - spring boot 2.1.x 是否支持 yaml 1.2 规范
- macos - 如何在mac中获得鼠标下的顶部窗口
- angular - 如何测试跨度的幻影?
- angular - 在 ngFor 选择选项中,如何在 onChange 发送特定对象属性的值
- node.js - 如何修复用于“google-cloud/translate”的模块nodeJS“googleauth.js”的“child_process”和“fs”依赖项