如何在选择器视图中居中使用工具栏按钮(swift 4)

问题描述 投票:0回答:1

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

let toolbar = UIToolbar()
toolbar.sizeToFit()
ios center toolbar swift4 picker
1个回答
1
投票

您只需在按钮之间添加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)
© www.soinside.com 2019 - 2024. All rights reserved.