您好,我在向导航 Controller 中包含的工具栏添加文本时遇到问题。这就是我想要做的:
let label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.text = "Delete Pins"
label.center = CGPoint(x: CGRectGetMidX(view.frame), y: view.frame.height)
label.textAlignment = NSTextAlignment.Center
let toolbarTitle = UIBarButtonItem(customView: label)
navigationController?.toolbar.setItems([toolbarTitle], animated: true)
我正在尝试将其添加到文本中以显示在此处,让用户知道他们处于删除模式,有什么帮助吗?
最佳答案
您需要激活您的工具栏:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setToolbarHidden(false, animated: false)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setToolbarHidden(true, animated: false)
}
您的 customView 看起来不错。您还可以使用和 flexibleSpace 设置左/右/中心:
let flexible = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
self.toolbarItems = [flexible,toolbarTitle]
关于ios - 将文本标签添加到工具栏 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38852045/