如何在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属性,但到目前为止似乎没有帮助。

为了使UIViewController透明,您必须呈现ModalViewController并将modalPresentationStyle的属性设置为UIModalPresentationOverCurrentContext

ModalViewController *mvc = [[ModalViewController alloc]initWithNibName:@"ModalViewController" bundle:[NSBundle mainBundle]];
    
mvc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    
 [self presentViewController:mvc animated:YES completion:nil];

ModalViewController中,您需要将backgroundcolor设置为清除,并将opaque设置为 false。

self.view.backgroundColor = [UIColor clearColor];
self.view.opaque = false;

如果要使ModalViewController透明,请根据需要设置backgroundcolor和不透明度。

最新更新