居中的 UITextField 文本在编辑时意外移动
Centered UITextField text moving unexpectedly on edit
我在 UICollectionView
单元格中有一个居中的 UITextField
,但文本在编辑时奇怪地向右移动。是什么导致了问题?
let textLabel: UITextField = {
let label = UITextField()
label.textAlignment = .center
label.adjustsFontSizeToFitWidth = true
label.minimumFontSize = 12
return label
}()
addSubview(textLabel)
textLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15).isActive = true
textLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
textLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
textLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -15).isActive = true
这是问题的屏幕截图:
更新:原来 UITextfield
的高度太大了,我降低了它的高度,现在一切正常。感谢您的帮助!
textLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15).isActive = true
textLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
textLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
textLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -15).isActive = true
我在 UICollectionView
单元格中有一个居中的 UITextField
,但文本在编辑时奇怪地向右移动。是什么导致了问题?
let textLabel: UITextField = {
let label = UITextField()
label.textAlignment = .center
label.adjustsFontSizeToFitWidth = true
label.minimumFontSize = 12
return label
}()
addSubview(textLabel)
textLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15).isActive = true
textLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
textLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
textLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -15).isActive = true
这是问题的屏幕截图:
更新:原来 UITextfield
的高度太大了,我降低了它的高度,现在一切正常。感谢您的帮助!
textLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15).isActive = true
textLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
textLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
textLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -15).isActive = true