簡體   English   中英

如何在iOS中顯示小型模態視圖控制器?

[英]How to display small, modal view controller in 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屬性,但是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設置為clear並將opaque設置為false。

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

如果要使您的ModalViewController透明,則根據需要設置backgroundcolor和opacity。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM