У меня есть UICollectionViewCell
, в котором есть UITextField
. Ячейки действуют как вкладки для приложения для создания заметок. Пользователь может нажать кнопку +, чтобы добавить вкладку, а затем может назвать ее. Это нормально работает для первой вкладки, но, как вы можете видеть ниже, курсор для UITextField
не помещается на последующих вкладках, и заголовки начинают глючить.

Я проверил, что кадры всех представлений находятся там, где они должны быть, и я попробовал функцию beginFloatingCursor(at:)
.
MainView: (удален неважный код)
class MainViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let tabCellId = "tabCellId"
var topTabView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.init(red: 236.0/250.0, green: 190.0/255.0, blue: 120.0/255.0, alpha: 1)
let tabHeight = view.frame.height / 30
let fromLeft = view.frame.width * 0.025
//self.view.frame.height / 27 is a hack for the top layout guide
let topLayoutGuideY = view.frame.height / 27
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
topTabView = UICollectionView(frame: CGRect(x: fromLeft, y: topLayoutGuideY, width: (view.frame.width * 0.95) - tabHeight, height: tabHeight), collectionViewLayout: layout)
topTabView.dataSource = self
topTabView.delegate = self
topTabView.backgroundColor = .clear
topTabView.isUserInteractionEnabled = true
topTabView.clipsToBounds = true
topTabView.register(tabCell.self, forCellWithReuseIdentifier: tabCellId)
view.addSubview(topTabView)
print(self.view.frame)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Tabs.SharedInstance.tabsToPersist.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: tabCellId, for: indexPath) as! tabCell
cell.setIndex(_index: indexPath.item)
guard let name = Tabs.SharedInstance.tabsToPersist[indexPath.item]?.tabName else {
cell.newTab()
return cell
}
cell.titleLabel.text = name
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: topTabView.frame.width / 7, height: topTabView.frame.height)
}
@objc func addTab(_ sender: UIButton) {
Tabs.SharedInstance.addTab(tab: Tab(index: Tabs.SharedInstance.tabsToPersist.count))
topTabView.reloadData()
print(Tabs.SharedInstance.tabsToPersist.count)
}
}
tabCell: (удален неважный код)
class tabCell: UICollectionViewCell, UITextFieldDelegate {
var titleLabel: UILabel
var titleInput: UITextField
override init(frame: CGRect) {
titleLabel = UILabel()
titleInput = UITextField()
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
titleLabel = UILabel()
titleInput = UITextField()
super.init(coder: aDecoder)
setupViews()
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.isHidden = true
Tabs.SharedInstance.tabsToPersist[index]?.tabName = textField.text
titleLabel.isHidden = false
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text, let textRangeToReplace = Range(range, in: text) else {
return false
}
let substringToReplace = text[textRangeToReplace]
let count = text.count - substringToReplace.count + string.count
return count <= 7
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.isHidden = false
titleLabel.isHidden = true
}
func newTab() {
titleInput.becomeFirstResponder()
titleInput.beginFloatingCursor(at: CGPoint(x: titleInput.frame.minX, y: titleInput.frame.minY))
}
fileprivate func setupViews(){
titleLabel.frame = frame
titleLabel.center = center
titleLabel.textAlignment = .center
titleInput.frame = frame
titleInput.center = center
titleInput.textAlignment = .center
titleInput.delegate = self
backgroundColor = .clear
addSubview(titleInput)
addSubview(titleLabel)
}
}