Вставьте UIView в UIToolbar - PullRequest
0 голосов
/ 25 мая 2018

Я хотел бы вставить 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 голосов
/ 25 мая 2018

Добавьте представление как 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() после установки его элементов.

...