user8105388
user8105388

Reputation:

how to center tool bar button on picker view (swift4)

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

Answers (1)

Maor
Maor

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

Related Questions