将UIView插入UIToolbar

时间:2018-05-25 17:09:56

标签: swift uiview uitoolbar

我想将UIView插入UIToolbar。 (见图) 我已经尝试过.addSubview但它不起作用。 感谢您提供的任何帮助!

 // Toolbar -> Done Butotn
    let toolbar = UIToolbar()
    toolbar.sizeToFit()

    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action:#selector(doneButtonClicked))

    toolbar.setItems([flexibleSpace, doneButton], animated: false)

    self.TextInput.inputAccessoryView = toolbar

enter image description here

1 个答案:

答案 0 :(得分:1)

将视图添加为从自定义视图创建的UIBarButtonItem

let someCustomView = ... // your custom view
let customItem = UIBarButtonItem(customView: someCustomView)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action:#selector(doneButtonClicked))

toolbar.setItems([customItem, flexibleSpace, doneButton], animated: false)

您应该在设置项目后致电toolbar.sizeToFit()