ios – How to Center A Button On A Nav Bar

0
181


I am trying to center an info button on the navigation controller. However, the info button only appears when the last filter is selected. My approach in centering the info button is to use two UIbutton; the first on the left and the other on the right. It looks okay for words with fewer characters; however, it pushes words with more characters to the extreme left. How do I fix this?

button.setImage(UIImage(systemName: "info.circle"), for: .normal)
let infoButton = UIBarButtonItem(customView: button)
infoButton.customView?.isHidden = viewModel.filter.toolTip == nil

button.addTarget(self, action: #selector(handleInfoButton), for: .touchUpInside)
let leftSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
leftSpace.width = 100
let rightSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
rightSpace.width = 60
let done = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donePressed))
navigationItem.rightBarButtonItems = [done,leftSpace,rightSpace,infoButton]

enter image description here
enter image description here