SWIFT – MODALPRESENTATIONSTYLE .OverCurrentContext results in a problem that the remote button is pressed on the rendered view controller

I have a problem when using .overCurrentContext modalPresentationStyle on the tvOS view controller:

let vc = UIStoryboard(name : "", bundle: Bundle.main).instantiateInitialViewController() //representative of actually presented VC
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil)

On the presented view controller, pressing the menu button will stop returning to the presented view controller. This also happens when you set it to .overFullScreen and .blurOverFullScreen. However, when you set it to. I did not encounter such problems when using currentContext or .fullScreen. Do I need to use any specific things when using certain 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)

So what happened here? The definesPresentationContext attribute was added in iOS 8, and the document explains the following:

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.

By setting the definesPresentationContext to YES, you can ensure that the controller to be presented is displayed in the original view controller’s Within the boundaries.

Happy coding!

I have a problem when using .overCurrentContext modalPresentationStyle on tvOS view controller:

let vc = UIStoryboard(name: "", bundle: Bundle.main).instantiateInitialViewController() //representative of actually presented VC
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: nil )

On the presented view controller, pressing the menu button will stop returning to the presenting view controller. This also happens when you set it to .overFullScreen and .blurOverFullScreen. However, when you change When it is set to .currentContext or .fullScreen, I have not encountered such problems. Do I need to use any specific things when using certain 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)

So what happened here? The definesPresentationContext attribute was added in iOS 8, and the document explains the following:

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.

By setting the definesPresentationContext to YES, you can ensure that the controller to be presented is displayed in the original view controller’s Within the boundaries.

Happy coding!

Leave a Comment

Your email address will not be published.