我想将工具栏上的按钮居中。目前,按钮自动转到右侧。
let toolbar = UIToolbar()
toolbar.sizeToFit()
您只需在按钮之间添加2个flexibleSpace即可。
例如:
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)