MDCTextField rightView property broken on iOS 13

2020-08-25 05:47发布

问题:

I can't use the rightView property with MDCTextField anymore on iOS 13. Am I the only one having an issue with this?

The right view width cover the whole text field: preventing the user interaction and hiding the textView content.

No problem when I switch from MDCTextField to UITextField.

回答1:

Add width constraint to the rightView/leftView.

Don't forget to set 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

You may notice some autolayout warnings in the consule because you didn't set the missing constraint for the rightView/leftView. So add missing constraints or simply ignore those.

And note that if the rightView/leftView is some kind of StackView, try to putting it inside a view and then add this view instead.



回答2:

Apparently this was a change in the way rightViewRect(forBounds:) behaves in iOS 13 Beta 5.

From the 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)

The MDCTextField -(CGRect)rightViewRectForBounds:(CGRect)bounds function needs to be updated.