'SearchController NavigationBar Hidden Error

I wanted to designate a search controller on the navigation bar so that the search bar could run without taking up space when pressing the button, but I had to press the button twice to run the search bar.

    private func setNavigation() {
            self.navigationController?.navigationBar.shadowImage = UIImage()
            self.navigationController?.navigationBar.isTranslucent = false
            self.navigationController?.navigationBar.barTintColor = .gray50
            searchBar.searchBar.delegate = self
    
            searchBtn.rx.tap.subscribe(onNext: {
                self.navigationItem.searchController = self.searchBar
                self.navigationItem.searchController?.searchBar.searchTextField.becomeFirstResponder()
                self.searchTableView.isHidden = false
            }).disposed(by: disposeBag)
    
            navigationItem.rightBarButtonItem = searchBtn
            navigationController?.navigationBar.tintColor = .black
        }
    
        func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
            navigationItem.searchController = nil
            self.searchTableView.isHidden = true
        }

So when I used the interval code, I could press it once. What's the reason for this?

    private func setNavigation() {
            self.navigationController?.navigationBar.shadowImage = UIImage()
            self.navigationController?.navigationBar.isTranslucent = false
            self.navigationController?.navigationBar.barTintColor = .gray50
            searchBar.searchBar.delegate = self
    
            searchBtn.rx.tap.subscribe(onNext: {
                self.navigationItem.searchController = self.searchBar
                Observable<Int>.interval(.seconds(0), scheduler: MainScheduler.instance)
                    .subscribe(onNext: { _ in
                        self.searchBar.searchBar.searchTextField.becomeFirstResponder()
                    }).disposed(by: self.disposeBag)
                self.searchTableView.isHidden = false
            }).disposed(by: disposeBag)
    
            navigationItem.rightBarButtonItem = searchBtn
            navigationController?.navigationBar.tintColor = .black
        }
    
        func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
            navigationItem.searchController = nil
            self.searchTableView.isHidden = true
        }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source