Prevent user from setting cursor position on UITextField

后端 未结 6 950
执念已碎
执念已碎 2020-12-17 17:03

I have an UITextField constructed using the storyboard. I want to not allow the user to change the position of the cursor and keep it always at the end of the text into the

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 17:34

    Disable any gesture recognizers on the text field after it has become first responder. This allows it to receive the initial tap, but prevents the user from interacting with the field while it is the first responder. This keeps the system behavior of keeping the cursor at the end of the text without allowing the user to override it.

    In a UITextField subclass, add the following:

    SWIFT 3.1:

    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return !isFirstResponder
    }
    

    In your storyboard, change the class of your text field to your UITextField subclass.

提交回复
热议问题