Oscar Cooke-Abbott
Oscar Cooke-Abbott

Reputation: 503

Remove UINavigationBar back button extra padding?

I've removed the back button's text by manually setting it to " " on each navigation item, however there is still extra padding between the button and the navigation item's title for no reason. enter image description here

Does anyone know how to get rid of this annoying spacing? In a few real case scenarios in my app, the title does concatenate as it gets slightly too long, even though it wouldn't need to if that space were not there.

Upvotes: 0

Views: 1353

Answers (2)

Piyush Sinroja
Piyush Sinroja

Reputation: 160

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    let arrayViews = (self.navigationController?.navigationBar.subviews)
    if let itemView = arrayViews?[1] {
        for lbl in itemView.subviews {
            lbl.frame = CGRect(x: -25, y: lbl.frame.origin.y, width: lbl.frame.size.width, height: lbl.frame.size.height)
        }
    }
}

Upvotes: 2

glyvox
glyvox

Reputation: 58029

You should create a custom UIBarButtonItem which uses popToViewController to go back to the previous item on your stack. That way, you can manually set the frame of your custom back button.

Upvotes: 0

Related Questions