这与UITextField rightView重叠右对齐文本类似,但我很难理解如何修复这个问题:

重叠发生在IOS13.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为中心,文本完全消失:

我能做些什么来修复我的代码吗?或者,如果我的代码看起来还好的话,我需要向苹果提交一个bug吗?
发布于 2019-10-11 07:15:29
Swift 5- iOS13
在iOS 13之前,UITextField假定其leftView和rightView的帧在分配时被正确设置,并且不会更改。
从iOS 13开始,leftViewRect(forBounds:)和rightViewRect(forBounds:)的实现现在要求查看它的systemLayoutSizeFitting(_:)。
若要在iOS 13上链接和运行之前的行为,请在视图上添加显式的大小调整约束,将其包装在普通的UIView中,或者将视图子类并实现systemLayoutSizeFitting(_:)。
textField类中的方法重写
override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.width - 50, y: 0, width: 30 , height: bounds.height)
}希望这对你有帮助。
发布于 2019-10-11 07:26:03
将宽度 constraint添加到rightView/leftView。
别忘了设置translatesAutoresizingMaskIntoConstraints = false
rightView.translatesAutoresizingMaskIntoConstraints = false
rightView.widthAnchor.constraint(equalToConstant: <#NeededWidth#>).isActive = true
// This is enough to make it act like before but you can set other missing constraints like height to suppress layout warnings and prevent further issues.
// rightView.heightAnchor.constraint(equalToConstant: <#HeightOfTheTextField#>).isActive = true您可能会注意到消费中的一些自动收费警告,因为您没有为rightView/leftView设置缺少的约束。因此,添加缺少的约束,或者简单地忽略这些约束。
请注意,如果rightView/leftView是某种StackView,请尝试将其放入view中,然后添加此视图。
-更多信息
来自iOS & iPadOS 13开发人员Beta 5发行说明
UIKit -解决的问题 在iOS 13之前,UITextField假定其leftView和rightView的帧在分配时被正确设置,并且不会更改。从iOS 13开始,leftViewRect(forBounds:)和rightViewRect(forBounds:)的实现现在为其systemLayoutSizeFitting(:)询问视图。若要在iOS 13上链接和运行之前的行为,请在视图上添加显式的大小调整约束,将其包装在普通的UIView中,或将视图子类化并实现systemLayoutSizeFitting(:)。(51787798)
发布于 2020-04-03 07:46:35
https://stackoverflow.com/questions/58335586
复制相似问题