ios - UITextField leftView 和 rightView 重叠问题 - iOS13

标签 ios uikit ios13

这类似于 UITextField rightView overlaps right align text 但我很难理解如何解决这个问题:

enter image description here

重叠发生在 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 和文本完全消失了: enter image description here

我可以做些什么来修复我的代码,或者如果我的代码看起来没问题,我是否需要向 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)
    }

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

希望对你有所帮助。

关于ios - UITextField leftView 和 rightView 重叠问题 - iOS13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58335586/

相关文章:

ios - UILabel 文本使用 NSTimer 更新,但在触摸/拖动 UITableView 时不更新(gif 示例)

ios - 从 FBConnect 迁移到 Facebook SDK

iOS 钥匙串(keychain) :strange behavior while setting and getting data in/out the keychain

ios - preferredStatusBarStyle 未被调用

swift - iOS 13 SwiftUI : app crashes upon launch on real device

ios - 如何以编程方式将 UINavigationController 添加到现有的 UIViewController

ios - 将插入符号拖动到 View 边缘时如何使我的 UITextView 自动滚动?

iphone - UIPageControl 不响应触摸,不改变点

ios - 如何在 iOS 13 或更高版本上使用 Sinch VOIP?

ios - 在新模拟器上运行时关闭 iOS 13 键盘教程