我想在工具栏上将按钮居中。当前,该按钮自动转到右侧。

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)

关于ios - 如何将工具栏按钮居中于uipickerview(swift 4),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47970300/

10-12 06:13