0

In my navigation bar, I have a right bar button item representing a search button. When I press on it, the following function gets called

    func didTapSearchButton(sender: Any) {
        navigationItem.rightBarButtonItems?.removeLast()
        navigationItem.titleView = searchController.searchBar
        self.searchController.searchBar.becomeFirstResponder()
    }

The problem with the following code is that the search bar added in the title view is not centered, i.e it's not aligned with the back button.

enter image description here

Something interesting that I've noticed is that if I comment out the call self.searchController.searchBar.becomeFirstResponder(), my title view stays centered.

Can anyone help?

andrei
  • 1,353
  • 15
  • 24
  • try this let textFieldOfSearchBar = self.searchController.searchBar.value(forKey: "searchField") as? UITextField textFieldOfSearchBar?.textAlignment = .center – Jigar Jun 07 '18 at 11:26
  • @JigarDarji I've tried, unfortunately it didn't work :( – andrei Jun 07 '18 at 11:57

0 Answers0