let vc = UIStoryboard(name: "", bundle: Bundle.main).instantiateInitialViewController() //representative of actually presented VC vc.modalPresentationStyle = .overCurrentContext present(vc, animated: true, completion: nil)
在呈现的视图控制器上,按下菜单按钮将停止返回到呈现视图控制器.将其设置为.overFullScreen和.blurOverFullScreen时也会发生这种情况.但是,在将其设置为.currentContext或.fullScreen时,我没有遇到此类问题.使用某些UIModalPresentationStyle时是否需要使用任何特定的东西?
let vc = UIStoryboard(name: "", bundle: Bundle.main).instantiateInitialViewController() //representative of actually presented VC vc.modalPresentationStyle = .overCurrentContext self.definesPresentationContext = true //*** adding this line should solve your issue *** self.present(vc, animated: true, completion: nil)
那么这里发生了什么?在iOS 8中添加了definesPresentationContext属性,文档说明了以下内容:
When a view controller is presented, iOS starts with the presenting view controller and asks it if it wants to provide the presentation context. If the presenting view controller does not provide a context, then iOS asks the presenting view controller’s parent view controller. iOS searches up through the view controller hierarchy until a view controller provides a presentation context. If no view controller offers to provide a context, the window’s root view controller provides the presentation context.
If a view controller returns YES, then it provides a presentation context. The portion of the window covered by the view controller’s view determines the size of the presented view controller’s view. The default value for this property is NO.
通过将definesPresentationContext设置为YES,可确保要呈现的控制器显示在原始视图控制器的边界内.
快乐的编码!