简体   繁体   English

如何在选择器视图(swift4)上将工具栏按钮居中

[英]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. 您只需在按钮之间添加2个flexibleSpace。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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