简体   繁体   中英

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()

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM