Disable blinking cursor in UITextField?

前端 未结 10 937
日久生厌
日久生厌 2020-12-24 06:31

I\'ve followed the instructions here and succesfully set up a UITextField that gets updated with a UIDatePicker. However the cursor in the UITextField is blinking, which see

相关标签:
10条回答
  • 2020-12-24 07:00

    I found this solution to be the easiest to implement.

    Make sure you define UITextFieldDelegate in your .h file:

    .... UIViewController <UITextFieldDelegate>
    

    In your .m file, add this to the method you call fo the date picker:

    [yourTextField resignFirstResponder];
    

    This will prevent the textfield from blinking.

    0 讨论(0)
  • 2020-12-24 07:01

    What I did was to overlay another UITextField on top of the one whose cursor I wanted to hide. Then in the delegate method textFieldShouldBeginEditing I set the other textField to become first responder and returned NO.

    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        if (textField.tag==TAG_OF_DUMMY_TEXTFIELD) {
            [otherField becomeFirstResponder];
            return NO;
        }
        return YES;
    }
    

    And then in the method the date picker calls:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@YOUR_DATE_FORMAT];
    dummyField.text = [dateFormatter stringFromDate:datePicker.date];
    

    In Interface Builder otherField (the one with the datePicker input view) is behind dummyField (the one that hides the cursor).

    0 讨论(0)
  • 2020-12-24 07:04

    Subclass UITextfield and Override the - (CGRect)caretRectForPosition:(UITextPosition *)position method and return CGRectZero.

    - (CGRect)caretRectForPosition:(UITextPosition *)position {
        return CGRectZero;
    }
    
    0 讨论(0)
  • 2020-12-24 07:04

    Not the best solution, but you could also set the Opacity of the tint color to 0%.

    0 讨论(0)
提交回复
热议问题