首页 > 解决方案 > 如何在 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物业,但到目前为止似乎没有帮助。

标签: iosuiviewuiviewcontrolleruikit

解决方案


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.


推荐阅读