iOS 模態(tài)推出半透明 ViewController 實(shí)現(xiàn)方法
模態(tài),ViewController,透明, 截圖彈窗
最近做項(xiàng)目時(shí),需求通過(guò)模態(tài)(modal)推出一個(gè)截屏的彈窗, 但是推出的模態(tài) VC 背景圖雖然設(shè)置了透明,但是推出 VC 界面的時(shí)候,VC 并沒(méi)有實(shí)現(xiàn)透明的效果,
剛開(kāi)始執(zhí)行時(shí)的代碼如下:
UIViewController *vc = [[[UIViewController alloc] init] autorelease];
vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
[self presentModalViewController:vc animated:YES];
通過(guò)上述代碼實(shí)現(xiàn),可以發(fā)現(xiàn)在動(dòng)畫(huà)過(guò)程中是半透明的,但是動(dòng)畫(huà)結(jié)束后就看不到下面一個(gè)viewController的內(nèi)容了匾浪,變黑色了蛋辈。
尋找其原因,解釋為:
he “problem” is that iOS is very finicky about not wasting memory,
and since the modal view will completely cover the one beneath it,
it doesn’t make much sense to keep it loaded.
Therefore, iOS unloads the view that presents the modal one.
You may check this behavior by implementing -viewWillDisappear: and -viewDidDisappear
有道詞典翻譯為:
iOS的“問(wèn)題”是非常講究不浪費(fèi)內(nèi)存,由于模態(tài)視圖將完全覆蓋下面的一個(gè),它沒(méi)有多大意義保持加載将谊。因此,iOS卸載的觀點(diǎn)介紹了模態(tài)。你可以檢查這個(gè)行為通過(guò)實(shí)現(xiàn)viewwilldisappear:-viewDidDisappear:
也就是說(shuō), 當(dāng)模態(tài)推出一個(gè) VC 界面后,下面的界面在去加載是沒(méi)有多大意義的,所以之前的界面將不在加載,所以沒(méi)能實(shí)現(xiàn)透明的效果.
在 stackoverflow 上有關(guān)于推出 VC 透明問(wèn)題的問(wèn)答大家可以參考
http://stackoverflow.com/questions/587681/how-to-use-presentmodalviewcontroller-to-create-a-transparent-view/5579145#5579145
但最后通過(guò)"簡(jiǎn)書(shū)"搜索,搜索到了關(guān)于透明 VC 的問(wèn)題:
最終實(shí)現(xiàn)效果如下:
ScreenShotViewController * shotVC = [ScreenShotViewController new];
//self is presenting view controller
self.definesPresentationContext = YES;
shotVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];
shotVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:shotVC animated:YES completion:nil];

參數(shù)詳解:
- definesPresentationContext
A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.
一個(gè)布爾值,用于顯示這個(gè)視圖控制器的視圖是否覆蓋當(dāng)視圖控制器或其后代提供了一個(gè)視圖控制器。
@property(nonatomic, assign) BOOL definesPresentationContext
UIModalPresentationOverCurrentContext style to present a view controller, this property controls which existing view controller in your view controller hierarchy is actually covered by the new content. When a context-based presentation occurs, UIKit starts at the presenting view controller and walks up the view controller hierarchy. If it finds a view controller whose value for this property is YES, it asks that view controller to present the new view controller. If no view controller defines the presentation context, UIKit asks the window’s root view controller to handle the presentation.
The default value for this property is NO. Some system-provided view controllers, such as UINavigationController, change the default value to YES.
當(dāng)使用 UIModalPresentationCurrentContext 或 UIModalPresentationOverCurrentContext 風(fēng)格呈現(xiàn)一個(gè)視圖控制器,該屬性控制現(xiàn)有的視圖控制器的視圖控制器層次結(jié)構(gòu)實(shí)際上是由新內(nèi)容虏劲。發(fā)生基于上下文的演講時(shí),UIKit開(kāi)始呈現(xiàn)視圖控制器和走到視圖控制器層次結(jié)構(gòu)褒颈。如果找到該屬性的一個(gè)視圖控制器,其價(jià)值是肯定的,它要求視圖控制器的視圖控制器。如果沒(méi)有視圖控制器定義了表示上下文,UIKit問(wèn)窗口的根視圖控制器來(lái)處理報(bào)告堡掏。這個(gè)屬性的默認(rèn)值是否定的刨疼。一些系統(tǒng)提供視圖控制器,如UINavigationController,改變默認(rèn)值為YES。