向工具栏添加文本标签 - Swift

时间:2016-08-09 13:22:34

标签: ios swift

您好我在导航控制器中包含的工具栏中添加文本时遇到问题。这就是我想要做的事情:

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)

我正试图将其显示在文本中以便让用户知道他们处于删除模式,有什么帮助吗?

1 个答案:

答案 0 :(得分:6)

您需要激活工具栏:

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]