Как добавить UICollectionView внутри UIAlertViewController?
Есть аналогичный вопрос о стека и потоке Как добавить UITableView в UIAlertView в быстрой и Как добавить UITableView внутри UIAlertView в iPhone? но я добавляю UICollectionView внутри UIAlertViewController и получаю сбой
Завершение работы приложения из-за необработанного исключения "NSInvalidArgumentException", причина: "-[UICollectionView view]: нераспознанный селектор, отправленный экземпляру 0x103808200"
Я использую ниже код
class NewProjectViewController: UIViewController {
var iconCollectionView:UICollectionView!
@IBAction func projectIconAction(_ sender: UIButton) {
selectIcon()
}
}
extension NewProjectViewController:UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Constant.iconArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath)
cell.backgroundColor = UIColor.blue
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
return CGSize(width: 50, height: 50)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
func selectIcon() {
let flowLayout = UICollectionViewFlowLayout()
iconCollectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
iconCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
iconCollectionView.delegate = self
iconCollectionView.dataSource = self
let alertController:UIAlertController = UIAlertController(title: "ICON", message: "", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Select", style: UIAlertActionStyle.default, handler: { (action) in
print("Icon Selected")
}))
alertController.setValue(iconCollectionView, forKey: "contentViewController")
alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
Пожалуйста, дайте мне знать, что я делаю не так здесь
Редактировать 1: Если я использую приведенный ниже код для добавления UICollectionViewController в качестве подпредставления к UIAlertController, то я получаю как на скриншоте
func selectIcon() {
let flowLayout = UICollectionViewFlowLayout()
iconCollectionView = UICollectionView(frame: CGRect(x: 0, y: 80, width: 300, height: 300), collectionViewLayout: flowLayout)
iconCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
iconCollectionView.delegate = self
iconCollectionView.dataSource = self
let alertController:UIAlertController = UIAlertController(title: "ICON", message: "", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Select", style: UIAlertActionStyle.default, handler: { (action) in
print("Icon Selected")
}))
// alertController.setValue(iconCollectionView, forKey: "contentViewController")
alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.destructive, handler: nil))
alertController.view.addSubview(iconCollectionView)
self.present(alertController, animated: true, completion: nil)
}
1 ответ
К сожалению, с Apple это невозможно UIAlertController
, Как показывают ответы на вопрос, который вы указали, для этого нужно создать собственное представление.
Вы можете сделать это самостоятельно (см. Другой ответ) или, альтернативно, существует множество библиотек, которые вы можете использовать.
Я лично использовал это раньше и имел большой успех с ним.
Удачи!