UITextField rightView sometimes positioned on the left after setting the text value

I have a button, with Auto layout size constraints, created in IB, which in code I have added as the rightView for a UITextField. Using iOS13.3 SDK and running on device and simulator, the button sometimes appears on the left after setting the textfield's text value.

This did not ever happen in the last 9 years since iOS4.

I have read the UIKit 'fix' in the BETA 5 release notes.


Has anyone else seen this behaviour or can suggest a workaround?


I have tried embedding the button in a UIView, with constraints, and adding the view as the rightView but the anomaly is still the same.

Replies

Does it occurs sometimes or all times ?

If sometimes, did you find a pattern ?


which in code I have added as the rightView for a UITextField.

Could you show the code ?

Did you set

textField.rightViewMode = .always

What are the constraints you have set for button : only size ?

It occurs sometimes but often - maybe 50%. If it is on the left and I set the text again it usually returns to the correct postion but can go wrong again. I have not found a pattern. I only set the rightView once. I tried setting it every time I set the text but that did not work.


On a device one can see the button flit to the left and then back to the right when it does not stay on the left.


rightViewMode is UITextFieldViewModeUnlessEditing


Constraints are size - width and height

With your present code,

rightViewMode is UITextFieldViewModeUnlessEditing

looks like :

- when editing, rightViewMode turns false

- and not always return true when editing finishes


How do you end Editing:

- by hitting return ?

- by selecting another field ?

Have you connected some sent events to the textField ? Which ?


So try to change

rightViewMode to UITextFieldViewModeAlways

Using UITextFieldViewModeAlways is not the behaviour I want.

In my app, the problem happens when I set the text, or if I edit and tap the clear button then the rightView button appeards in the left of the text field


I have submitted a bug report with a simple app that demonstrates

1) On 13.3 simulator, but not iOS12 device, the rightView button is placed in the left of the UITextField at startup . It should be in the right

2) Tapping the button causes the text to be set and the rightViewbutton then moves to the right - simulator

3) Editing the field and tapping return key causes the rightView button to return to the left - simulator and iOS12 iPhone (I implemented textFieldShouldReturn and resigned first responder)

4) Editing the field and tapping the clear button causes the rightViewbutton to appear in the left -simulator and device


These problems appear to happen on the simulator and iOS12 iPhone only if there are size constraints on the button, which Beta 5 release notes specifically said add.

However, in my real app removing the size constraints on the button does not fix the problem.

Which beta 5 (XCode 11.x ?)

Apparently iOS13 Developer Beta 5 (according to a post in StackView in late September 2019). Earlier posts suggest it was Beta 3 or 4.


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)


I'm trying to find the release notes which I have seen

Did you set the text field's placeholder?

I had the same issue with the rightView. I fixed it by removing the placeholder.
If placeholder text is not so important, I suggest removing it.

In my case, I create a custom button with a CAShapeLayer. I set the layer's path in layoutSubviews(), and then the bug appear.Just as you said, When I tap the button, it run to the textField's leftView, while the textField became the first responser, it run back the rightView.

So I state a var to control the layout, and the issue resolved.

private var layout = false

override func layoutSubviews() {
        super.layoutSubviews()
        //
        if self.layout {
            return

        }
        self.layout = true
        //do layout
}