这类似于 UITextField rightView overlaps right align text 但我很难理解如何解决这个问题:
重叠发生在 ios 13.1 上 我设置 borderStyle = .none 如果存在边框,则文本不会与 图片。
public func addRightImage(_ image: UIImage, accessibilityIdentifier: String? = nil,
size: CGSize) {
self.rightViewMode = .always
let widthImageView : CGFloat = 25
let rightImageView = UIImageView(frame: .init(x: 0, y: 0, width: widthImageView, height: self.bounds.size.height))
rightImageView.accessibilityIdentifier = accessibilityIdentifier
rightImageView.image = image
if #available(iOS 13, *) {
self.rightView = rightImageView
} else {
assert(self.rightImageView == nil)
let widthView : CGFloat = is4incher ? 30 : 50
let rightView = UIView(frame: .init(x: 0, y: 0, width: widthView, height: self.bounds.size.height))
rightView.isUserInteractionEnabled = false
self.rightView = rightView
rightView.addSubview(rightImageView)
rightView.isUserInteractionEnabled = false
self.rightView = rightView
rightImageView.anchors(centerX: rightView.centerXAnchor, centerY: rightView.centerYAnchor,
size: size)
}
rightImageView.set(color: self.textColor ?? .white)
rightImageView.contentMode = .scaleAspectFit
self.rightImageView = rightImageView
}
如果我摆脱了
if #available(iOS 13, *) {
self.rightView = rightImageView
然后进入 ios12 路径,图像居中于 UITextField 和文本完全消失了:
我可以做些什么来修复我的代码,或者如果我的代码看起来没问题,我是否需要向 apple 提交错误?
最佳答案
Swift 5 - iOS13
Prior to iOS 13, UITextField assumed that the frames of its leftView and rightView were correctly set when assigned and would never change.
从 iOS 13 开始,leftViewRect(forBounds:)
和 rightViewRect(forBounds:)
的实现现在向 View 询问其 systemLayoutSizeFitting(_:)
。
要在针对 iOS 13 链接和运行时实现之前的行为,请在 View 上添加显式大小限制,将其包装在普通 UIView 中,或者将 View 子类化并实现 systemLayoutSizeFitting(_:)
textField 类中的方法覆盖
override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.width - 50, y: 0, width: 30 , height: bounds.height)
}
希望对你有所帮助。
关于ios - UITextField leftView 和 rightView 重叠问题 - iOS13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58335586/