首页 > 解决方案 > 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.

enter image description here

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.

enter image description here

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.

标签: iosuinavigationcontrollertransparencypushviewcontroller

解决方案


您必须展示您的控制器,因为此 modalPresentationStyle 和 modalTransitionStyle 只会在您选择呈现模态方法时才会影响。确保保持半控制器主视图的背景颜色清晰或不透明

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

推荐阅读