2

I have Navigation Controller and 2 Viewcontrollers: MainVC(root) and SearchVC.

Here is the hierarchy: Vc's hierarchy

First of all I'm using this transition function from MainVC ("+" navigation item) to SearchVC:

@objc func goToSearchVC() {

        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let searchVC = storyBoard.instantiateViewController(withIdentifier: "SearchVC") as! SearchVC

        //Hide system navigation controller back button and add custom close button
        DispatchQueue.main.async {
            searchVC.navigationItem.hidesBackButton = true
            searchVC.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "closeBtn_20"), style: .plain, target: self, action: #selector(self.closeBtnTapped))
            searchVC.navigationItem.title = ""
        }

        //It's constructed function for custom transition type, in this case transition from top, it's works well
        animatedTransition(navi: self.navigationController!,transitionType: kCATransitionFromTop, duration: 0.5)

        //push to SearchVC
        self.navigationController?.pushViewController(searchVC, animated: false)

    }

    //close button function for SearchVC navibar
    @objc public func closeBtnTapped() {
         animatedTransition(navi: self.navigationController!, transitionType: kCATransitionFromBottom, duration: 0.5)
        navigationController?.popViewController(animated: false)

        let mainNaviBar = self.navigationController!.navigationBar
        mainNaviBar.isHidden = true
    }

This "+" push transition and close button "x" pop transition works well:

Transition from MainVc

In SearchVC I'm using GMSAutocompleteResultsViewController for searching places and trying pop to MainVC using goToVc() function (I checked the performance of all 3 "pop" options in turn, but they do not work):

upd: self.view.window!.rootViewController?.dismiss(animated: false, completion: nil) doesn't work too

class SearchVC: UIViewController, CLLocationManagerDelegate {

            var resultsViewController: GMSAutocompleteResultsViewController?
            var searchController: UISearchController?
            var resultView: UITextView?

            override func viewDidLoad() {
                super.viewDidLoad()


                //Make navigation bar visible
                let mainNaviBar = self.navigationController?.navigationBar
                mainNaviBar?.isHidden = false


                resultsViewController = GMSAutocompleteResultsViewController()
                resultsViewController?.delegate = self

                searchController = UISearchController(searchResultsController: resultsViewController)
                searchController?.searchResultsUpdater = resultsViewController

                // Put the search bar in the navigation bar.
                searchController?.searchBar.sizeToFit()
                navigationItem.titleView = searchController?.searchBar

                // When UISearchController presents the results view, present it in
                // this view controller, not one further up the chain.
                definesPresentationContext = true

                // Prevent the navigation bar from being hidden when searching.
                searchController?.hidesNavigationBarDuringPresentation = false

            }//viewDidLoad()

            //function for segue to existing VC, MainVC
            func goToVc() {

    //It's constructed function for custom transition type, in this case transition from top, it's works well
animatedTransition(navi:self.navigationController!,transitionType: kCATransitionFromTop, duration: 0.5)   

       //!!!I checked the performance of all these "pop" options in turn, but they do not work

              //Option 1, popToRootViewController (MainVC) - Doesn't work
    self.navigationController?.popToRootViewController(animated: false)

     //Option 2, popViewController(animated: false) - Doesn't work
    self.navigationController?.popViewController(animated: false)

     //Option 3, popToViewController - Doesn't work
    self.navigationController?.popToViewController(MainVC(), animated: false)
            }

        }//class

    Extension:

        // Handle the user's selection.
        extension SearchVC: GMSAutocompleteResultsViewControllerDelegate {
            func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                                   didAutocompleteWith place: GMSPlace) {
                searchController?.isActive = false
                // Do something with the selected place.
                print("Place name: \(place.name)")
                print("Place address: \(place.formattedAddress)")
                print("Place attributions: \(place.attributions)")
                print("Place coordinats:\(place.coordinate)")


                //segue to MainVC
                goToVc()

            }
            func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
                                   didFailAutocompleteWithError error: Error){
                // TODO: handle the error.
                print("Error: ", error.localizedDescription)
            }

            // Turn the network activity indicator on and off again.
            func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
                UIApplication.shared.isNetworkActivityIndicatorVisible = true
            }

            func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
                UIApplication.shared.isNetworkActivityIndicatorVisible = false
            }
        }

In all three "pop to MainVC " cases transition to MainVC not happening, and it pop-ed to current VC:

upd: self.view.window!.rootViewController?.dismiss(animated: false, completion: nil) have the same effect

Go back to MainVC

How can I fix this, and have the transition to MainVC??

Rurom
  • 243
  • 3
  • 16

0 Answers0