We have common views that we use in our application in many locations inside of UINavigationControllers
. Occasionally the UINavigationController
s are inside of popover views. Now the views we put into the nav controllers modify their navigation controller's toolbar buttons and, in some cases, use custom buttons that we've created. We need to be able to figure out from the UIViewcontroller
itself if the view is inside of a popoverview so we can display the correctly colored buttons.
We can easily get the Navigation controller reference from the UIViewController, using UIViewController.navigationController
, but there doesn't seem to be anything for finding a UIPopoverController
.
Does anyone have any good ideas for how to do this?
Thanks!
Swift 4 version (function can be added in
extension UIViewController
):In case that someone else is still looking for a solution i came up with one good enough for me.
Just override this method
Here is an example of YourViewController
If it is popover it will not call 'viewControllerForAdaptivePresentationStyle' method and it will stay true, in case it is not popover it will set it to false.
In iOS8 you can use popoverPresentationController property of UIViewController to check if it is contained in a popover presentation controller. From documentation, it returns: "The nearest ancestor in the view controller hierarchy that is a popover presentation controller. (read-only)"
By working with SpareTime's code I came to this, which works as expected. Nice code, nice solution:
Using the standard UISplitViewController example.
All the solutions above seems a little bit complicated. I'm using a variable called
isInPopover
which I set to true if the view controller is presented in a popover. In the view controller inpopoverControllerDidDismissPopover
or inviewWillDisappear
I set the boolean value to false. It does work and is very simple.Modification of the accepted answer for iOS5.1 and newer:
** NOTE **
See comments about the reliability of this code.