閱讀388 返回首頁    go 阿裏雲 go 技術社區[雲棲]


在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:.

最終在SO上找到這麼個問題,以及一份可行的方案

viewController.view.backgroundColor = [UIColor clearColor];
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[rootViewController presentModalViewController:viewController animated:YES];

這裏有兩個點:一是設置modalPresentationStyle為UIModalPresentationCurrentContext,二是需要在rootViewController上操作。

最後更新:2017-04-03 12:53:36

  上一篇:go 手機衛士10-手機被盜後定位實現
  下一篇:go 手機衛士11-手機鎖屏和出廠恢複功能