我正在尝试将文本输入到 Collection View 单元格中,每当我编辑第二个单元格时, Collection View 就会在键盘显示时滚动到第一个单元格,这是 Storyboard 上的设置:
这是文本编辑的代码:
func addText() {
let textView = UITextView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: bounds.width * 0.75, height: bounds.height * 0.75)))
textView.center = shapeView.center
textView.delegate = self
textView.font = .systemFont(ofSize: 32)
shapeView.addSubview(textView)
imageButton.removeFromSuperview()
textButton.removeFromSuperview()
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}
有人可以告诉我为什么 View 会自动滚动以及如何禁用它吗?
最佳答案
我发现了这个问题,奇怪的是,这是因为 Collection View 的尺寸过大,正如您在添加的屏幕截图中看到的那样,它比 iPhone 和 iPad 上的屏幕宽度都大。
虽然我仍然不知道为什么会发生这种情况,但我通过将前导和尾随约束设置到view
的边缘来解决这个问题,而不是设置固定宽度。
感谢大家的回答!
关于ios - 编辑另一个单元格时, Collection View 自动跳转到第一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44061670/