嗨,我在将文本添加到导航 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/

10-11 17:18
查看更多