Prevent editing of text in UITextField and hide cursor/caret/magnifying glass while using an inputView

后端 未结 6 1644
北荒
北荒 2021-02-13 01:40

How do I prevent the editing of text in a UITextField along with hiding the cursor/caret/magnifying glass, but still present the keyboard, as I am using an in

6条回答
  •  悲哀的现实
    2021-02-13 02:28

    To have a UITextField without interaction, but still work with an inputView:

    Subclass UITextField with these methods:

    // Hide the cursor
    - (CGRect)caretRectForPosition:(UITextPosition*)position
    {
        return CGRectZero;
    }
    
    // All touches inside will be ignored
    // and intercepted by the superview
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        return NO;
    }
    

    The last method will single-handedly prevent any editing and the magnifier glass, as you won't be able to tap the UITextField.

    This works great if you're using a textfield in a UITableViewCell for example, and then can toggle firstResponder status through tableView:didSelectRowAtIndexPath:.

提交回复
热议问题