【问题标题】:UITextField leftView and rightView overlapping issue - iOS13UITextField leftView 和 rightView 重叠问题 - iOS13
【发布时间】:2019-10-11 06:42:35
【问题描述】:

这类似于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 和文本完全消失了:

我可以做些什么来修复我的代码,或者如果我的代码看起来没问题,我是否需要向苹果提交错误?

【问题讨论】:

  • 我对 ios13 有类似的问题。在我的自定义文本文件类中覆盖 rightViewRectForBounds 的 rightview 方法后,这对我有用

标签: ios uikit ios13


【解决方案1】:

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)
    }

参考链接- https://howtotechglitz.com/apple-has-just-released-ios-13-developer-beta-5-for-iphone-ios-iphone-gadget-hacks/

希望对你有帮助。

【讨论】:

    【解决方案2】:

    宽度 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
    

    您可能会在 consule 中注意到一些自动布局警告,因为您没有为 rightView/leftView 设置缺少的约束。所以添加缺失的约束或者干脆忽略那些。

    请注意,如果rightView/leftView 是某种StackView,请尝试将其放在view 中,然后添加此视图。

    - 更多信息

    来自iOS & iPadOS 13 Developer Beta 5 Release Notes

    UIKit - 已解决的问题

    在 iOS 13 之前,UITextField 假定其 leftView 和 rightView 的框架在分配时已正确设置并且永远不会更改。从 iOS 13 开始,leftViewRect(forBounds:) 和 rightViewRect(forBounds:) 的实现现在向视图询问其 systemLayoutSizeFitting(:)。要在 iOS 13 上链接和运行时实现之前的行为,请在视图上添加显式大小约束,将其包装在普通 UIView 中,或子类化视图并实现 systemLayoutSizeFitting(:)。 (51787798)

    【讨论】:

    • 这是唯一对我有用的方法,谢谢!!
    【解决方案3】:
    1. 确保设置为 ImageView 的图像与 ImageView 的大小相同。这是最简单的方法,只需调整图像大小即可。

    2. 重写 rightViewRect() 方法并根据需要提供大小。

    【讨论】: