gpt4 book ai didi

IOS 13 : spacing Issue with UITextField rightView

转载 作者:行者123 更新时间:2023-12-01 23:44:10 26 4
gpt4 key购买 nike

我在 IOS 13 中遇到 UITextField 右 View 的间距问题,请参阅我的以下代码以及 ios 13 和 ios 12.4 的屏幕截图

在 IOS 12.4 模拟器中,在 UITextField 的右 View (UIButton)中显示适当的空间

在 IOS 13.0 模拟器中,UITextField 的右 View (UIButton)存在间距问题

let dropdownButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: txtField.frame.height))
dropdownButton.backgroundColor = UIColor.clear
dropdownButton.setImage(UIImage(named: "ic_DownArrow"), for: UIControl.State())
txtField.rightView = dropdownButton
txtField.rightViewMode = .always

enter image description here enter image description here

最佳答案

显然这是 rightViewRect(forBounds:) 在 iOS 13 Beta 5 中的行为方式的变化。

来自 iOS 和 iPadOS 13 开发者 Beta 5 发行说明:

UIKit - Resolved Issues

Prior to iOS 13, UITextField assumed that the frames of its leftView and rightView were correctly set when assigned and would never change. Starting in iOS 13, the implementation of leftViewRect(forBounds:) and rightViewRect(forBounds:) now ask the view for its systemLayoutSizeFitting(:). To achieve the previous behavior when linking against and running on iOS 13, add explicit sizing constraints on the view, wrap it in a plain UIView, or subclass the view and implement systemLayoutSizeFitting(:). (51787798)

因此,将自动布局约束添加到您添加到 rightView 的自定义 View

示例:-

override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.width - 30, y: 0, width: 20 , height: bounds.height)
}

关于IOS 13 : spacing Issue with UITextField rightView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58166160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com