简体   繁体   中英

Push new UIViewController with transparency

I have a very simple project with a view controller embedded in a navigation controller. I want to push a new view controller with transparency which in effect allows parts of the underlying controller to show through.

在此处输入图像描述

During the push transition, parts of the underlying controller indeed show through the new controller that is being pushed, but when the transition finishes, the underlying controller view is removed from the view hierarchy and the transparent part of the new controller shows up in black.

在此处输入图像描述

According to Apple ,

When presenting a view controller using the UIModalPresentationFullScreen style, UIKit normally removes the views of the underlying view controller after the transition animations finish. You can prevent the removal of those views by specifying the UIModalPresentationOverFullScreen style instead. You might use that style when the presented view controller has transparent areas that let underlying content show through.

I do specify the modalPresentationStyle of the presented (pushed) view controller to be UIModalPresentationOverFullScreen, during the button action that initialises the push:

let semiController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SemiViewController")
    semiController.modalPresentationStyle = .overFullScreen
    self.navigationController?.pushViewController(semiController, animated: true)

I don't think I have to specify a custom transition for this to work. So what gives?

Thanks.

You have to present your controller as this modalPresentationStyle and modalTransitionStyle will only affect if you choose present modally method. Make sure to keep the semicontroller main view's background color clear or less in opacity

let semiController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SemiViewController")
semiController.modalPresentationStyle = .overCurrentContext
semiController.modalTransitionStyle = .crossDissolve
self.present(semiController, animated: true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM