如何将 UICollectionView 添加到 inputAccessoryView
How to add UICollectionView to the inputAccessoryView
我想将 UICollectionView
添加到 inputAccessoryView
以便滚动列表。
override var inputAccessoryView: UIView? {
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 80))
customView.backgroundColor = UIColor.red
return customView
}
添加一个UICollectionView
作为inputAccessoryView
,
1. 首先创建一个包含 UICollectionView
的 custom UIView
然后添加 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
的实例添加为 UITextField/UITextView
的 inputAccessoryView
。
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
数据。
我想将 UICollectionView
添加到 inputAccessoryView
以便滚动列表。
override var inputAccessoryView: UIView? {
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 80))
customView.backgroundColor = UIColor.red
return customView
}
添加一个UICollectionView
作为inputAccessoryView
,
1. 首先创建一个包含 UICollectionView
的 custom UIView
然后添加 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
的实例添加为 UITextField/UITextView
的 inputAccessoryView
。
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
数据。