Tuesday, January 26, 2010

Disabling keyboard on UITextField selection

There are often cases where you want the look of UITextField but don't want to use the keyboard. There are a few hacky ways such as hidden buttons overlaying text fields, but I think this is the most intelligent way to disable the show of the UIKeyboard, and replacing it with something of your choice:


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
// Make a new view, or do what you want here
UIDatePicker *pv = [[UIDatePicker allocinitWithFrame:CGRectMake(0,185,0,0)];
[self.view addSubview:pv];

return NO;
}

You'll need to be implementing UITextFieldDelegate, and you can catch this event prior to the framework showing the UIKeyboard. In the above example I am showing a UIDatePicker instead. Returning NO is what is making the keyboard not show, since we aren't actually letting the editing begin.

1 comment:

  1. Just wanted to thank you for the tip. I was using UITextField's inputView property instead, but it's only supported in iPhone OS 3.2+.

    ReplyDelete