Description
In iOS, not able to update the cursor position manually from the started event using the "SelectedTextRange" property on focus. Please refer to the below sample and screenshot.
Steps to Reproduce
- Create a custom control.
- Create native control by inheriting from UITextField.
- Hook started the event in native.
- Use the below code snippet to update the cursor position from the started event. Code Snippet:
var positionToSet = this.GetPosition(this.BeginningOfDocument, 3); if (positionToSet != null) { this.SelectedTextRange = this.GetTextRange(positionToSet, positionToSet); }
(OR)
- Run the attached sample.
- Cursor should be positioned in the 3rd index (next to 3) while focusing on the control.
Expected Behavior
The cursor should be positioned at the third index (next to 3) as shown in the below screenshot.
Actual Behavior
The cursor gets positioned at the last.
Sample: https://github.com/xamarin/Xamarin.Forms/files/7653944/CustomControl.zip
Note: Issue reproduced only on or above iOS device 14.4.
CodePudding user response:
Its necessary to queue the change so that it occurs after Started event returns:
CoreFoundation.DispatchQueue.MainQueue.DispatchAsync( () => {
this.SelectedTextRange = this.GetTextRange(positionToSet, positionToSet);
});
Based on Leo Dabus' answer in swift.
Tested on iOS simulator, iPhone 11, iOS 15.0.