Search code examples
iosswiftuinavigationcontrolleruisearchbar

UISearchbar squished in UINavigationbar


I have a search bar set as the title view in my navigation bar. In the initial view the search bar is evenly spaced in the middle with buttons on both sides. In this view the back button pushes the search bar so that it is uneven. I've tried getting rid of the text of the back button, but it still didn't work to spread the search bar out. Squished search bar image


Solution

  • I recently needed to solve the same issue. I ended up creating a UIBarButtonItem with the back button image asset, and setting that as the navigationItem.leftBarButtonItem. This button press then calls navigationController?.popViewController(animated: true) to pop the viewController off the stack