Getting and Setting Cursor Position of UITextField and UITextView in Swift

后端 未结 4 1018
遥遥无期
遥遥无期 2020-11-22 17:10

I\'ve been experimenting with UITextField and how to work with it\'s cursor position. I\'ve found a number of relation Objective-C answers, as in

4条回答
  •  佛祖请我去吃肉
    2020-11-22 17:33

    in my case I had to use DispatchQueue:

    func textViewDidBeginEditing(_ textView: UITextView) {
    
       DispatchQueue.main.async{
          textField.selectedTextRange = ...
       }
    }
    

    nothing else from this and other threads worked.

    PS: I double checked which thread did textViewDidBeginEditing was running on, and it was main thread, as all UI should run on, so not sure why that little delay using main.asynch worked.

提交回复
热议问题