A lot of people have been saying this is a bug, but being that this problem still exists in the GM I'm starting to think it might be a change in logic. With that said, I wrote this bit of code for my app and have tested it on iOS 7-8.
This code is slightly before the red line of private API's, however you should have no problem using it. My app with this code is in the app store.
Add the following method to your UITextField
subclass.
- (BOOL)keyboardInputShouldDelete:(UITextField *)textField {
BOOL shouldDelete = YES;
if ([UITextField instancesRespondToSelector:_cmd]) {
BOOL (*keyboardInputShouldDelete)(id, SEL, UITextField *) = (BOOL (*)(id, SEL, UITextField *))[UITextField instanceMethodForSelector:_cmd];
if (keyboardInputShouldDelete) {
shouldDelete = keyboardInputShouldDelete(self, _cmd, textField);
}
}
BOOL isIos8 = ([[[UIDevice currentDevice] systemVersion] intValue] == 8);
BOOL isLessThanIos8_3 = ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.3f);
if (![textField.text length] && isIos8 && isLessThanIos8_3) {
[self deleteBackward];
}
return shouldDelete;
}
To explain a little, were calling the super's implementation of this method to avoid losing inherited code. After were going to call -deleteBackward
if there is no text and the iOS version is between 8-8.2.
EDIT: 1/28/15
It also might be helpful to subclass the -deleteBackward method of your subclassed UITextField. This fixes a few conditional bugs. One being if you use a custom keyboard. Heres an example of the method.
- (void)deleteBackward {
BOOL shouldDismiss = [self.text length] == 0;
[super deleteBackward];
if (shouldDismiss) {
if ([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
[self.delegate textField:self shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""];
}
}
}
EDIT: answer translated to Xamarin, as the original question asked this for Xamarin.
[Preserve]
[Export("keyboardInputShouldDelete:")]
private bool KeyboardInputShouldDelete(UITextField textField)
{
var shouldDelete = true;
if(RespondsToSelector(new Selector("_cmd")))
{
//Call base class
shouldDelete = Messaging.bool_objc_msgSend_IntPtr(Handle, Selector.GetHandle("_cmd"), textField.Handle);
}
//ios8 "bug": always call DeleteBackward even if the field is empty
if(Utils.IsIos8)
{
DeleteBackward();
return false;
}
return shouldDelete;
}
Verified on ios 7.1 and 8.1
EDIT: 4/14/15
As of iOS 8.3 this issue has been fixed. The Objective-C code has been updated to reflect the changes.
EDIT: 7/24/15
As @nischalhada commented, a state exists where the text fields -textField:shouldChangeCharactersInRange:replacementString:
is called twice. The problem exists on iOS >= 8.3 while using a custom keyboard. My solution is not ideal but it does the job and I'm not sure if there's any other way. Since both calls to this method are performed on the same run loop we'll use a bool to keep track of when to execute the code and a dispatch async to reset the bool.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL toReturn = NO;
if (!self.shouldTextFieldPreventChange) {
self.shouldTextFieldPreventChange = YES;
dispatch_async(dispatch_get_main_queue(), ^{
// iOS8.3 custom keyboards might call this method along with internal iOS
// code. Allowing changes on the next run loop helps avoid this issue.
self.shouldTextFieldPreventChange = NO;
});
// do work...
}
return toReturn;
}