diff --git a/Tusker/Screens/Explore/ExploreViewController.swift b/Tusker/Screens/Explore/ExploreViewController.swift index f15ff818..75c58f65 100644 --- a/Tusker/Screens/Explore/ExploreViewController.swift +++ b/Tusker/Screens/Explore/ExploreViewController.swift @@ -59,7 +59,7 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate, Collect resultsController = SearchResultsViewController(mastodonController: mastodonController) resultsController.exploreNavigationController = self.navigationController! - searchController = MastodonSearchController(searchResultsController: resultsController) + searchController = MastodonSearchController(searchResultsController: resultsController, owner: self) definesPresentationContext = true navigationItem.searchController = searchController diff --git a/Tusker/Screens/Explore/InlineTrendsViewController.swift b/Tusker/Screens/Explore/InlineTrendsViewController.swift index 9eefaaab..74ab5e36 100644 --- a/Tusker/Screens/Explore/InlineTrendsViewController.swift +++ b/Tusker/Screens/Explore/InlineTrendsViewController.swift @@ -34,7 +34,7 @@ class InlineTrendsViewController: UIViewController { resultsController = SearchResultsViewController(mastodonController: mastodonController) resultsController.exploreNavigationController = self.navigationController - searchController = MastodonSearchController(searchResultsController: resultsController) + searchController = MastodonSearchController(searchResultsController: resultsController, owner: self) searchController.obscuresBackgroundDuringPresentation = true searchController.hidesNavigationBarDuringPresentation = false definesPresentationContext = true diff --git a/Tusker/Screens/Search/MastodonSearchController.swift b/Tusker/Screens/Search/MastodonSearchController.swift index b35eb034..bc627645 100644 --- a/Tusker/Screens/Search/MastodonSearchController.swift +++ b/Tusker/Screens/Search/MastodonSearchController.swift @@ -24,7 +24,11 @@ class MastodonSearchController: UISearchController { super.searchResultsController as! SearchResultsViewController } - init(searchResultsController: SearchResultsViewController) { + private weak var owner: UIViewController? + + init(searchResultsController: SearchResultsViewController, owner: UIViewController) { + self.owner = owner + super.init(searchResultsController: searchResultsController) searchResultsController.tokenHandler = { [unowned self] token, op in @@ -152,6 +156,12 @@ extension MastodonSearchController: UISearchBarDelegate { } } +extension MastodonSearchController: MultiColumnNavigationCustomTargetProviding { + var multiColumnNavigationTargetViewController: UIViewController? { + owner + } +} + extension UISearchBar { var searchQueryWithOperators: String { var parts = searchTextField.tokens.compactMap { $0.representedObject as? String } diff --git a/Tusker/Screens/Utilities/MultiColumnNavigationController.swift b/Tusker/Screens/Utilities/MultiColumnNavigationController.swift index 96576a61..b11bdd17 100644 --- a/Tusker/Screens/Utilities/MultiColumnNavigationController.swift +++ b/Tusker/Screens/Utilities/MultiColumnNavigationController.swift @@ -8,6 +8,13 @@ import UIKit +/// View controllers, such as `UISearchController`, that live outside the normal VC hierarchy +/// can adopt this protocol to indicate to `MultiColumnNavigationController` the context for +/// navigation operations. +protocol MultiColumnNavigationCustomTargetProviding { + var multiColumnNavigationTargetViewController: UIViewController? { get } +} + class MultiColumnNavigationController: UIViewController { private var isManuallyUpdating = false @@ -99,6 +106,8 @@ class MultiColumnNavigationController: UIViewController { index = _viewControllers.firstIndex(of: c) if index != nil { break + } else if let targetProviding = c as? MultiColumnNavigationCustomTargetProviding { + current = targetProviding.multiColumnNavigationTargetViewController } else { current = c.parent }