Swift UICollectionViewCell内的UITextField成为第一响应程序
我知道这个问题可能有很多答案。但在尝试了互联网上的每一个解决方案(我的一些定制发明)之后,我仍然无法实现我想要实现的目标 故事是这样的: 我有一个UICollectionViewCell,其中嵌入了UITextField的子类 下面是我的子类:Swift UICollectionViewCell内的UITextField成为第一响应程序,swift,uicollectionview,uitextfield,uicollectionviewcell,Swift,Uicollectionview,Uitextfield,Uicollectionviewcell,我知道这个问题可能有很多答案。但在尝试了互联网上的每一个解决方案(我的一些定制发明)之后,我仍然无法实现我想要实现的目标 故事是这样的: 我有一个UICollectionViewCell,其中嵌入了UITextField的子类 下面是我的子类: class CustomTextField: UITextField { private let padding = UIEdgeInsets(top: 6.0, left: 0.0, bottom: 0.0, right: 2.0)
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 我可能没有正确的答案,但我认为问题在于您将手机放在收集视图(didSelectItemAt:)中的方式 您使用的是
dequeueReusableCell
而不是cellForItem(at:)
来获取单元格。因此,您正在创建一个可重用单元,而没有得到您感兴趣的单元