在iOS上present一個半透明的viewController

來源:互聯網
上載者:User

今天嘗試著在一個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上操作。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.