【问题标题】:MDCTextField rightView property broken on iOS 13MDCTextField rightView 属性在 iOS 13 上损坏
【发布时间】:2020-01-24 11:04:00
【问题描述】:

我不能再在 iOS 13 上将 rightView 属性与 MDCTextField 一起使用。只有我一个对此有问题吗?

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

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

【问题讨论】:

标签: ios swift ios13 material-components


【解决方案1】:

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

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

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

【讨论】:

  • 我在原来的 UITextField 上遇到过这个问题(我认为是从 beta 3 开始),在执行上述方法后,它已修复。
  • 谢谢。实际上,向textField.rightView 添加约束看起来是最干净的解决方法。我将用动态宽度约束替换手动调整大小的框架。
  • 其实这是一种显式的实现方式。自动布局通常适用于几乎任何东西。请不要忘记分配赏金,并随时提出任何进一步的问题。
  • 刚刚做了。在自动布局也可以与 textField.rightView 一起使用之前我没有注意到。
  • 我还不知道为什么,但看起来与右侧视图按钮子视图的交互通过宽度自动布局不再起作用...
【解决方案2】:

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

来自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)

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

【讨论】: