UIColletionViewCell 中的 UITextField 成为 FirstResponder

UITextField inside UIColletionViewCell becomeFirstResponder

我知道这个问题可能有很多答案。但是在尝试了在互联网上找到的所有解决方案之后(加上我的一些自定义发明..)我仍然无法实现我想要实现的目标。

这是故事:

我有一个 UICollectionViewCell,其中嵌入了一个 UITextField 的子类。

这是我的子类:

class CustomTextField: UITextField {

    private let padding = UIEdgeInsets(top: 6.0, left: 0.0, bottom: 0.0, right: 2.0)

    private lazy var lineView: UIView = {
        let lineView = UIView()
        lineView.translatesAutoresizingMaskIntoConstraints = false
        lineView.isUserInteractionEnabled = false
        lineView.frame.size.height = 2
        lineView.backgroundColor = UIColor.tiara
        return lineView
    }()

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }
    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    func toggleColors() {
        if isFirstResponder {
            lineView.backgroundColor = .black
        } else {
            lineView.backgroundColor = UIColor.tiara
        }
    }
}

private extension CustomTextField {
    func commonInit() {
        addSubview(lineView)
        constraintLineView()
        textColor = UIColor.tiara
    }

    func constraintLineView() {
        lineView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        lineView.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        lineView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        lineView.heightAnchor.constraint(equalToConstant: 2.0).isActive = true
    }
}

这是我在 UICollectionViewCell 中使用的代码:

@discardableResult
func setFirstResponder() -> Bool {
    return customTextField.becomeFirstResponder()
}

func endEditing() {
    customTextField.resignFirstResponder()
}

customTextField.becomeFirstResponder 的结果总是 false

它是从我的 UIViewController 调用的:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    guard indexPath.row != 0 else { return }

    dispatchService.stop()
    let topIndexPath = IndexPath(item: 0, section: 0)

    let topCell: Cell = collectionView.dequeueReusableCell(for: topIndexPath)
    topCell.endEditing()
    service.data.rearrange(from: indexPath.row, to: 0)
    update()

    collectionView.performBatchUpdates({
        collectionView.moveItem(at: indexPath, to: topIndexPath)
        collectionView.scrollToItem(at: topIndexPath, at: .top, animated: true)
    }) { (_) in
        let secondCell: Cell = collectionView.dequeueReusableCell(for: topIndexPath)
        secondCell.setFirstResponder()
        self.dispatchService.reset()
    }
}

我真的不知道从哪里开始,这是我提出的最后一个解决方案,它仍然没有显示任何键盘。

我正在使用真实设备,iPhone X iOS 12.1。

我可能没有安静的正确答案,但我认为问题出在您将手机放入内部的方式 collectionView(didSelectItemAt:)

您正在使用 dequeueReusableCell 而不是 cellForItem(at:) 来获取您的细胞。因此,您正在创建一个可重复使用的单元格,但没有得到您感兴趣的单元格。