Note:
- I am not using a
UINavigationController
. - I have a
UIViewController
calledContainerViewController
which has 3 child view controllers that acts similar to aUIPageViewController
, but it is not aUIPageViewController
, it's contains a pagingUIScrollView
instead. Each child view controller then is able to instantiate other view controller's modally.
I want to refresh a view controller when coming back from the background. However, this should only happen if when you return from the background, that view controller is the visible one (meaning top most and active).
Question is: How do I determine if a view controller is the one that is visible (top most and active on screen now)?
If I use an answer from this question: Get top most UIViewController
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
This works. But if I leave the app in a child view controller of ContainerViewController
, this extension just returns ContainerViewController
-> not the child view controller.
Any thoughts?