2

I used a custom navigation controller class to remove the back button in my navigation bar, with the following:

class CustomNavigationController: UINavigationController, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {

        viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
    }

}

In one of my VCs, I also add a search controller to the navigation bar with the following:

private func configureSearchController() {
        self.searchController = UISearchController(searchResultsController: nil)
        self.searchController.searchResultsUpdater = self
        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self

        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = true

        self.navigationItem.titleView = searchController.searchBar
        self.definesPresentationContext = true
    }

However, it seems like the search bar is still being offset by some blank space where the title would be. I'd image the search bar should be aligned next to the back button, but there is the back button, space with blank title, then search bar.

enter image description here

How can I get the search bar aligned next to the back button instead of spaced because it seems there is some room for the blank title. Also, as a note, tapping on that blank space moves me back to the previous VC, although there is no actual title there.

EDIT:

View hierarchy, looks like the back button container view itself takes up a good amount of space enter image description here

Brejuro
  • 3,421
  • 8
  • 34
  • 61
  • 1
    did you check this [answer](https://stackoverflow.com/a/20986544/6689101) or the one below it in swift – zombie Feb 19 '18 at 18:45
  • @zombie Yes, and still had the same problem – Brejuro Feb 19 '18 at 18:47
  • then I would suggest to check the layout to see what is happening so go to the top menu -> Debug -> View Debugging -> Capture View Hierarchy – zombie Feb 19 '18 at 18:55
  • @zombie Wow I actually didn't know about this feature, but I also don't really know what I'm looking for with this in order to solve my problem. It looks like the `UIBackButtonContainerView` is what's taking up the space – Brejuro Feb 19 '18 at 19:32
  • that means the back button is still taking a big space. try to check the rest of the answer in question I posted above – zombie Feb 19 '18 at 19:37
  • @zombie doesn't seem like many of these help, i tried a majority of these fixes and my search bar is still not aligned next to my back button – Brejuro Feb 19 '18 at 19:50

1 Answers1

0

Try adding this in viewDidLoad

let backButton = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
backButton.setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, -1000), for: .default)
navigationItem.backBarButtonItem = backButton
zombie
  • 5,069
  • 3
  • 25
  • 54