今天嘗試著在一個ViewController上面調用:
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
來展示一個半透明的viewController:
UIViewController *vc = [[[UIViewController alloc] init] autorelease]; vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; [self presentModalViewController:vc animated:YES];
這樣可以發現在動畫過程中是半透明的,但是動畫結束後就看不到下面一個viewController的內容了,變黑色了。
為什麼呢。搜尋了一番得到一份比較合理的結論:
The “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:.
最終在SO上找到這麼個問題,以及一份可行的方案:
viewController.view.backgroundColor = [UIColor clearColor];rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;[rootViewController presentModalViewController:viewController animated:YES];
這裡有兩個點:一是設定modalPresentationStyle為UIModalPresentationCurrentContext,二是需要在rootViewController上操作。