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 seems quite a bit awkward to me. Is there any solution to get rid of that cursor?
10 Answers
I couldn't get jcm's solution to work. What I ended up doing was to subclass UILabel to mimic a UITextField's interactive functionality without the parts that I didn't want (like the cursor). I wrote a blog post about it here:
http://pietrorea.com/2012/07/how-to-hide-the-cursor-in-a-uitextfield/
Basically, the UILabel subclass needs to overwrite isUserInteractionEnabled, inputView, inputViewAccessory and canBecomeFirstResponder. It's only a few lines of code and it makes more sense.
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).
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.
Balaji's approach does work.
I also used such KVC solutions many times. Despite it seems to be undocumented, but it works. Frankly, you don't use any private methods here - only Key-Value Coding which is legal.
It is drastically different from [addNewCategoryTextField textInputTraits].
P.S. Yesterday my new app appeared at AppStore without any problems with this approach. And it is not the first case when I use KVC in changing some read-only properties (like navigatonBar) or private ivars.
You can add a BOOL cursorless property to UITextField in a category via associated objects.
@interface UITextField (Cursorless)
@property (nonatomic, assign) BOOL cursorless;
@end
Then use method swizzling to swizzle caretRectForPosition: with a method that toggles between CGRectZero and its default value using cursorless.
This leads to a simple interface via a drop-in category. This is demonstrated in the following files.
Simply drop them in and get the benefit of this simple interface
UITextField category:
https://github.com/rexmas/RexDK/blob/master/RexDK/UI/UITextField%2BRXCursorless.h
https://github.com/rexmas/RexDK/blob/master/RexDK/UI/UITextField%2BRXCursorless.m
Method Swizzling: https://github.com/rexmas/RexDK/blob/master/RexDK/Foundation/NSObject%2BRXRuntimeAdditions.h https://github.com/rexmas/RexDK/blob/master/RexDK/Foundation/NSObject%2BRXRuntimeAdditions.m

