modalPresentationStyle .overCurrentContext导致在呈现的视图控制器上按下远程按钮时出现问题

时间:2017-11-17 16:17:28

标签: swift uiviewcontroller tvos uimodalpresentationstyle

我在tvOS视图控制器上使用.overCurrentContext modalPresentationStyle时出现问题:

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时,是否需要使用某些特定内容?

1 个答案:

答案 0 :(得分:4)

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中添加了definePresentationContext属性,文档中指出以下内容:

  

呈现视图控制器时,iOS从呈现的视图控制器开始,并询问它是否要提供呈现上下文。如果呈现视图控制器未提供上下文,则iOS会询问呈现视图控制器的父视图控制器。 iOS在视图控制器层次结构中向上搜索,直到视图控制器提供演示文稿上下文为止。如果没有视图控制器提供上下文,则窗口的根视图控制器将提供呈现上下文。

     

如果视图控制器返回YES,则它将提供表示上下文。视图控制器的视图所覆盖的窗口部分确定了所呈现的视图控制器的视图的大小。此属性的默认值为NO。

通过将definePresentationContext设置为YES,可以确保要显示的控制器显示在原始视图控制器的范围内。

编码愉快!