【问题标题】:Collection View as Custom Keyboard not working集合视图作为自定义键盘不起作用
【发布时间】:2019-08-08 09:07:06
【问题描述】:

我正在构建具有自定义键盘的应用程序。

在它的类里面我创建了集合视图,这里是代码:

class KeyboardViewController: UIInputViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate {

let stickerImages = [
    UIImage(named: "Image-1"),
    UIImage(named: "Image-2"),
    UIImage(named: "Image-3"),
    UIImage(named: "Image-4"),
    UIImage(named: "Image-5")
]

@IBOutlet var nextKeyboardButton: UIButton!
@IBOutlet var collectionView: UICollectionView!

override func updateViewConstraints() {
    super.updateViewConstraints()
}

override func viewDidLoad() {
    super.viewDidLoad()

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.scrollDirection = UICollectionView.ScrollDirection.vertical
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: 50, height: 50)

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.register(StickersCell.self, forCellWithReuseIdentifier: StickersCell.reuseIdentifier)
    collectionView.backgroundColor = UIColor.white
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.backgroundColor = UIColor.red

    collectionView.translatesAutoresizingMaskIntoConstraints = false

    self.view.addSubview(collectionView)

    self.nextKeyboardButton = UIButton(type: .system)

    self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: [])
    self.nextKeyboardButton.sizeToFit()
    self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
    self.nextKeyboardButton.backgroundColor = UIColor.white

    self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)

    self.view.addSubview(self.nextKeyboardButton)

    self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
    self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

    self.collectionView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
    self.collectionView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
    self.collectionView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
    self.collectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}

override func textWillChange(_ textInput: UITextInput?) {
    // The app is about to change the document's contents. Perform any preparation here.
}

override func textDidChange(_ textInput: UITextInput?) {
    // The app has just changed the document's contents, the document context has been updated.

    var textColor: UIColor
    let proxy = self.textDocumentProxy
    if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
        textColor = UIColor.white
    } else {
        textColor = UIColor.black
    }
    self.nextKeyboardButton.setTitleColor(textColor, for: [])
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return stickerImages.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: StickersCell.reuseIdentifier, for: indexPath) as! StickersCell

    cell.setImage(stickerImages[indexPath.item]!)

    return cell
}}

这是我的 Collection View 单元格类:

class StickersCell: UICollectionViewCell {

static let reuseIdentifier: String = "StickersCell"

lazy var imageView: UIImageView = {
    let imageView = UIImageView(frame: .zero)
    imageView.contentMode = .scaleAspectFit
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    contentView.clipsToBounds = true
    contentView.addSubview(imageView)

    imageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
    imageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
    imageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
    imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setImage(_ image: UIImage) {
    imageView.image = image
}}

在将集合视图添加到任何 UIViewUIViewController 时,此代码可以正常工作,但在尝试将其添加到键盘时会引发此类错误:

据我所知,我放置了错误的约束,但我不明白到底出了什么问题,尤其是当它在简单视图或视图控制器中正常工作时。

我用谷歌搜索了分配,但找不到任何解决方案... 这个 SO 问题也没有帮助:

  1. First question
  2. Second question
  3. Third question

我还尝试将创建集合视图的代码移动到 viewDidAppearviewWillAppear 方法中,但同样没有运气。

另一个奇怪的事情: 如果我向键盘添加具有相同约束的简单UIView - 一切正常。问题似乎与集合视图有关。

那么,我错过了什么?将不胜感激任何帮助,因为我已经与这个问题作斗争一个多星期了......

更新:

在阅读了 Apple 开发者论坛之后,我想到了一个想法: 我之前创建了与UICollectionView 相同的UITableView,但奇怪的是它可以工作。于是就有了下一个问题: 您可以将UICollectionView 用作自定义键盘吗?

【问题讨论】:

  • 它告诉您在 UIViewAlertForUnsatisfiableConstraints 处设置断点以在调试器中捕获它
  • @mohsen 我添加了断点,但没有错误...也许我添加错了?我使用了本指南 - stackoverflow.com/a/34855001/8945677

标签: ios swift uicollectionview keyboard custom-keyboard


【解决方案1】:

在与这个问题斗争了 2 周后,终于找到了可行的解决方法:

由于某种原因,您不能在 UICollectionViewCell 中使用 UIImageViewMSStickerView,这与在 iMessage 扩展中相同,所以我只是在此按钮内添加了透明的 UIButtonUIImage,它就可以工作了!

仍然不知道为什么您不能使用图像或视图,也找不到任何有关它的具体信息,但我的解决方案有效,我希望这对将来的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多