Чтобы добавить UICollectionView
как inputAccessoryView
,
1. Создайте custom UIView
, содержащий UICollectionView
, и добавьте UICollectionViewDataSource
методы к нему.
class CustomView: UIView, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
let words = ["abscind","downwind","headwind","lind","rescind","sind","skinned","tailwind","thin-skinned","tinned","twinned","upwind","whirlwind","wind"]
override func awakeFromNib() {
super.awakeFromNib()
self.collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "cell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.words.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
cell.label.text = self.words[indexPath.row]
return cell
}
}
class CustomCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
}
2. Добавьте экземпляр выше созданного CustomView
как inputAccessoryView
из UITextField/UITextView
согласно вашему требованию.
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
if let customView = Bundle.main.loadNibNamed("CustomView", owner: self, options: nil)?.first as? CustomView {
self.textField.inputAccessoryView = customView
}
}
}
В приведенном выше коде вы можете настроить collectionView
с необходимыми данными.