Reputation:
I want to center the button on the toolbar. Currently the button automatically goes to the right hand side.
let toolbar = UIToolbar()
toolbar.sizeToFit()
Upvotes: 1
Views: 416
Reputation: 3430
You can simply add 2 flexibleSpace between your button.
for example:
let toolbar = UIToolbar(frame: CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: view.bounds.width, height: 44))
var items = [UIBarButtonItem]()
items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
items.append( UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(test)) )
items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
toolbar.items = items
self.view.addSubview(toolbar)
Upvotes: 2