在iOS上present一個半透明的viewController
今天嚐試著在一個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:.
viewController.view.backgroundColor = [UIColor clearColor]; rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext; [rootViewController presentModalViewController:viewController animated:YES];
這裏有兩個點:一是設置modalPresentationStyle為UIModalPresentationCurrentContext,二是需要在rootViewController上操作。
最後更新:2017-04-03 12:53:36