首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITextField leftView和rightView重叠问题- iOS13

UITextField leftView和rightView重叠问题- iOS13
EN

Stack Overflow用户
提问于 2019-10-11 06:42:35
回答 3查看 2.6K关注 0票数 1

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

重叠发生在IOS13.1上,当我设置borderStyle = .none时,如果存在边框,则文本不重叠图像。

代码语言:javascript
复制
    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
    }

如果我摆脱了

代码语言:javascript
复制
    if #available(iOS 13, *) {
        self.rightView = rightImageView

然后进入ios12路径,图像以UITextField为中心,文本完全消失:

我能做些什么来修复我的代码吗?或者,如果我的代码看起来还好的话,我需要向苹果提交一个bug吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 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类中的方法重写

代码语言:javascript
复制
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/

希望这对你有帮助。

票数 5
EN

Stack Overflow用户

发布于 2019-10-11 07:26:03

宽度 constraint添加到rightView/leftView

别忘了设置translatesAutoresizingMaskIntoConstraints = false

代码语言:javascript
复制
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)

票数 2
EN

Stack Overflow用户

发布于 2020-04-03 07:46:35

  1. 确保设置为ImageView的图像与ImageView大小相同。这将是最简单的方法,只是调整图像的大小。
  2. 重写rightViewRect()方法,并根据需要提供一个大小。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58335586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档