ios – UiNavigationController – title in navigation bar is off-center when right side has 2 buttons


I’m using a UINvagigationController for my app, with every page setting their own title and left and right buttons.
Worked perfectly until I needed two buttons to the right. Just setting them as bar button items throws the title off-center, and the space between them is way bigger then expected:
enter image description here

This is how I set up those buttons to the right:

    let item1 = UIBarButtonItem(customView: self.btnGoToScan)
    let item2 = UIBarButtonItem(customView: self.btnExportImage)
    self.navigationItem.setRightBarButtonItems([item1, item2], animated: false)

I have tried setting the width of those items, as well as adding fixed space of negative width between them – changes nothing:

let item1 = UIBarButtonItem(customView: self.btnGoToScan)
    item1.width = 24
    let item2 = UIBarButtonItem(customView: self.btnExportImage)
    item2.width = 24
    let negativeSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    negativeSpace.width = -16
    self.navigationItem.setRightBarButtonItems([item1, negativeSpace, item2], animated: false)

How can I make the right bar items close and the title always centered? Like this:
enter image description here

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img