MDCTextField rightView 属性 在 iOS 13 上损坏

MDCTextField rightView property broken on iOS 13

我不能再在 iOS 上将 rightView 属性 与 MDCTextField 一起使用 13。我是唯一对此有疑问的人吗?

右视图宽度覆盖整个文本字段:阻止用户交互并隐藏 textView 内容。

当我从 MDCTextField 切换到 UITextField 时没问题。

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

来自iOS & iPadOS 13 Developer Beta 5 Release Notes

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)

MDCTextField-(CGRect)rightViewRectForBounds:(CGRect)bounds函数需要更新。

宽度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.widthAnchor.constraint(equalToConstant: <#HeightOfTheTextField#>).isActive = true

您可能会注意到领事馆中的一些自动布局警告,因为您没有为 rightView/leftView 设置缺少的约束。因此,添加缺少的约束或干脆忽略它们。

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